Java 筆記試験手書きアルゴリズム面接の質問と回答の完全なコレクション

(*-*)浩
リリース: 2019-11-07 15:49:30
オリジナル
3004 人が閲覧しました

Java 筆記試験手書きアルゴリズム面接の質問と回答の完全なコレクション

#1. 英語の記事の単語数を数えます。

public class WordCounting {
    public static void main(String[] args) {
        try(FileReader fr = new FileReader("a.txt")) {
            int counter = 0;
            boolean state = false;
            int currentChar;
            while((currentChar= fr.read()) != -1) {
                if(currentChar== ' ' || currentChar == '\n'
                        || currentChar == '\t' || currentChar == '\r') {
                    state = false;
                }
                else if(!state) {
                    state = true;
                    counter++;
                }
            }
            System.out.println(counter);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
ログイン後にコピー

補足: このプログラムはさまざまな方法で記述できます。ここで選択したコードは、デニス M. リッチー先生とブライアン W. カーニハン先生の不朽の本「The C Programming Language」の中で与えられたコードです。 、二人の先生に敬意を表します。以下のコードも同様です。

2. 年、月、日を入力し、その日付が何日であるかを計算します。

public class DayCounting {
    public static void main(String[] args) {
        int[][] data = {
                {31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                {31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
        };
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入年月日(1980 11 28): ");
        int year = sc.nextInt();
        int month = sc.nextInt();
        int date = sc.nextInt();
        int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];
        int sum = 0;
        for(int i = 0; i < month -1; i++) {
            sum += daysOfMonth[i];
        }
        sum += date;
        System.out.println(sum);
        sc.close();
    }
}
ログイン後にコピー

3. 回文素数: いわゆる回文数は、前後に読んでも同じ数字です (例: 11、121、1991...)。数値は両方です 回文数値は素数 (1 とそれ自体でしか割り切れない数値) です。 11 から 9999 までの回文素数を見つけるプログラム。

public class PalindromicPrimeNumber {
    public static void main(String[] args) {
        for(int i = 11; i <= 9999; i++) {
            if(isPrime(i) && isPalindromic(i)) {
                System.out.println(i);
            }
        }
    }
    public static boolean isPrime(int n) {
        for(int i = 2; i <= Math.sqrt(n); i++) {
            if(n % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static boolean isPalindromic(int n) {
        int temp = n;
        int sum = 0;
        while(temp > 0) {
            sum= sum * 10 + temp % 10;
            temp/= 10;
        }
        return sum == n;
    }
}
ログイン後にコピー

4. 完全な順列: 5 つの数字のすべての順列を 12345 とします。

public class FullPermutation {
    public static void perm(int[] list) {
        perm(list,0);
    }
    private static void perm(int[] list, int k) {
        if (k == list.length) {
            for (int i = 0; i < list.length; i++) {
                System.out.print(list[i]);
            }
            System.out.println();
        }else{
            for (int i = k; i < list.length; i++) {
                swap(list, k, i);
                perm(list, k + 1);
                swap(list, k, i);
            }
        }
    }
    private static void swap(int[] list, int pos1, int pos2) {
        int temp = list[pos1];
        list[pos1] = list[pos2];
        list[pos2] = temp;
    }
    public static void main(String[] args) {
        int[] x = {1, 2, 3, 4, 5};
        perm(x);
    }
}
ログイン後にコピー

5. N 個の整数要素を持つ 1 次元配列の場合、その部分配列 (配列内の連続した添え字を持つ要素で構成される配列) の合計の最大値を求めます。 。

いくつかの例を以下に示します (最大のサブ配列は太字です):

Array: { 1, -2, 3, 5 , -3, 2 }、結果は: 8

2) 配列: { 0, -2, 3, 5, -1, 2 }、結果は: 9

3 ) 配列: { -9, -2,-3, -5, -3 }、結果: -2

は動的計画法を使用して解決できます:

public class MaxSum {
    private static int max(int x, int y) {
        return x > y? x: y;
    }
    public static int maxSum(int[] array) {
        int n = array.length;
        int[] start = new int[n];
        int[] all = new int[n];
        all[n - 1] = start[n - 1] = array[n - 1];
        for(int i = n - 2; i >= 0;i--) {
            start[i] = max(array[i], array[i] + start[i + 1]);
            all[i] = max(start[i], all[i + 1]);
        }
        return all[0];
    }
    public static void main(String[] args) {
        int[] x1 = { 1, -2, 3, 5,-3, 2 };
        int[] x2 = { 0, -2, 3, 5,-1, 2 };
        int[] x3 = { -9, -2, -3,-5, -3 };
        System.out.println(maxSum(x1)); // 8
        System.out.println(maxSum(x2)); // 9
        System.out.println(maxSum(x3)); //-2
    }
}
ログイン後にコピー

6. 再帰を使用して文字列反転を実装します

public class StringReverse {
    public static String reverse(String originStr) {
        if(originStr == null || originStr.length()== 1) {
            return originStr;
        }
        return reverse(originStr.substring(1))+ originStr.charAt(0);
    }
    public static void main(String[] args) {
        System.out.println(reverse("hello"));
    }
}
ログイン後にコピー

7. 正の整数を入力し、それを素数の積に分解します。

public class DecomposeInteger {
    private static List<Integer> list = new ArrayList<Integer>();
    public static void main(String[] args) {
        System.out.print("请输入一个数: ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        decomposeNumber(n);
        System.out.print(n + " = ");
        for(int i = 0; i < list.size() - 1; i++) {
            System.out.print(list.get(i) + " * ");
        }
        System.out.println(list.get(list.size() - 1));
    }
    public static void decomposeNumber(int n) {
        if(isPrime(n)) {
            list.add(n);
            list.add(1);
        }
        else {
            doIt(n, (int)Math.sqrt(n));
        }
    }
    public static void doIt(int n, int div) {
        if(isPrime(div) && n % div == 0) {
            list.add(div);
            decomposeNumber(n / div);
        }
        else {
            doIt(n, div - 1);
        }
    }
    public static boolean isPrime(int n) {
        for(int i = 2; i <= Math.sqrt(n);i++) {
            if(n % i == 0) {
                return false;
            }
        }
        return true;
    }
}
ログイン後にコピー

8. n 歩あります。一度に 1、2、または 3 歩歩くことができます。n 歩後には何通り歩くことができますか?

public class GoSteps {
    public static int countWays(int n) {
        if(n < 0) {
            return 0;
        }
        else if(n == 0) {
            return 1;
        }
        else {
            return countWays(n - 1) + countWays(n - 2) + countWays(n -3);
        }
    }
    public static void main(String[] args) {
        System.out.println(countWays(5)); // 13
    }
}
ログイン後にコピー

9. 英単語のすべての文字がすべて異なるかどうかを判断するアルゴリズムを作成します (大文字と小文字は区別されません)

public class AllNotTheSame {
    public static boolean judge(String str) {
        String temp = str.toLowerCase();
        int[] letterCounter = new int[26];
        for(int i = 0; i <temp.length(); i++) {
            int index = temp.charAt(i)- &#39;a&#39;;
            letterCounter[index]++;
            if(letterCounter[index] > 1) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        System.out.println(judge("hello"));
        System.out.print(judge("smile"));
    }
}
ログイン後にコピー

10. 重複した要素を含むソートされた整数配列があります。重複した要素を削除してください。たとえば、A= [1, 1, 2, 2, 3]。処理される配列は A = [1, 2] である必要があります。 、3]。

public class RemoveDuplication {
    public static int[] removeDuplicates(int a[]) {
        if(a.length <= 1) {
            return a;
        }
        int index = 0;
        for(int i = 1; i < a.length; i++) {
            if(a[index] != a[i]) {
                a[++index] = a[i];
            }
        }
        int[] b = new int[index + 1];
        System.arraycopy(a, 0, b, 0, b.length);
        return b;
    }
    public static void main(String[] args) {
        int[] a = {1, 1, 2, 2, 3};
        a = removeDuplicates(a);
        System.out.println(Arrays.toString(a));
    }
}
ログイン後にコピー

11. 配列の半分以上を占める重複要素がある場合、この要素を見つけます。

public class FindMost {
    public static <T> T find(T[] x){
        T temp = null;
        for(int i = 0, nTimes = 0; i< x.length;i++) {
            if(nTimes == 0) {
                temp= x[i];
                nTimes= 1;
            }
            else {
                if(x[i].equals(temp)) {
                    nTimes++;
                }
                else {
                    nTimes--;
                }
            }
        }
        return temp;
    }
    public static void main(String[] args) {
        String[]strs = {"hello","kiss","hello","hello","maybe"};
        System.out.println(find(strs));
    }
}
ログイン後にコピー

12. 文字列のバイト長を見つけるメソッドを作成しますか?

public int getWordCount(String s){
    int length = 0;
    for(int i = 0; i < s.length(); i++)
    {
        int ascii = Character.codePointAt(s, i);
        if(ascii >= 0 && ascii <=255)
            length++;
        else
            length += 2;
    }
    return length;
}
ログイン後にコピー

以上がJava 筆記試験手書きアルゴリズム面接の質問と回答の完全なコレクションの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!