最も一般的ですがエラーが発生しやすい8つのアルゴリズムの質問は、ほとんどの場合、インタビューでテストされます。テストでいくつ答えることができますか?

序文

新しいフレームワークを繰り返した後でも、何年にもわたって、常に刺激的な味わいを醸し出しているいくつかの質問があります。以下の筆記試験の質問は、JAVAのインタビューでよく見られます。それらを覚えておく必要がありますが、レビューするときにそれらを伝えることができません。私はこの種の損失に苦しんでいます、ましてや、トピックを見てみましょう。

1.2次元配列で検索

面接の質問

2次元配列(各1次元配列の長さは同じ)では、各行は左から右に昇順でソートされ、各列は上から下に昇順でソートされます。関数を完成させ、そのような2次元配列と整数を入力して、配列に整数が含まれているかどうかを判別してください。

コード

public class Test7 {
    
    

	public static void main(String[] args) {
    
    
		int[][] array = new int[][] {
    
    {
    
    1,2},{
    
    2,3},{
    
    3,4}};
		boolean find1 = find(3, array);
		boolean find2 = find(8, array);
		System.out.println(find1); // 输出true
		System.out.println(find2); // 输出 false

	}
	
	/**
	 * @param target
	 * @param array
	 * @return
	 */
	public static boolean find(int target, int [][] array) {
    
    
		int row = 0;
		int col = array[0].length-1;
		
		while(row<array.length && col>=0){
    
    
        if(array[row][col] == target)
            return true;
        else if(array[row][col] > target)
            col-=1;
        else
            row+=1;
		}
		return false;
    }
	

}

2.リンクリストの質問

面接の質問

リンクリストを入力し、リンクリストの最後から最初の順にArrayListを返します

コード

class ListNode {
    
    
    
	int val;        
	ListNode next = null;
	ListNode(int val) {
    
    
	       this.val = val;   
	   }
	}

public class Test8 {
    
    

	public static void main(String[] args) {
    
    
		ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(new ListNode(10));
		System.out.println(printListFromTailToHead.size());
		for (Integer integer : printListFromTailToHead) {
    
    
			System.out.println(integer);
		}

	}
	
	/**
	 * 
	 * @param listNode
	 * @return
	 */
	 public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    
    
		 
	        ArrayList<Integer> arr = new ArrayList<Integer>();
	        ListNode p = listNode;
	        ArrayList<Integer> stack = new ArrayList<Integer>();
	        while(p!=null){
    
    
	            stack.add(p.val);
	            p = p.next;
	        }
	        int n = stack.size();
	        for(int i=n-1;i>=0;i--){
    
    
	            arr.add(stack.get(i));
	        }
	        return arr;
	    }

}

3.キューイングの質問

面接の質問

2つのスタックを使用してキューを実装し、キューのプッシュ操作とポップ操作を完了します。キュー内の要素はint型です

コード

public class Test9 {
    
    
	
    static Stack<Integer> stack1 = new Stack<Integer>();
    static Stack<Integer> stack2 = new Stack<Integer>();
    
    public static void main(String[] args) {
    
    
    		push(1);
    		push(2);
    		push(3);
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
    		pop();
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
	}
    
    public static void push(int node) {
    
    
         stack1.push(node);
    }
    
    /**
     * pop操作 复杂
     * @return
     */
    public static int pop() {
    
    
    	int temp;
        
    	while(!stack1.empty()){
    
    
            temp = stack1.pop();
            stack2.push(temp);
        }
        
        int res = stack2.pop();
        while(!stack2.empty()){
    
    
            temp = stack2.pop();
            stack1.push(temp);
        }
        return res;
    }}

4.アレイに関する質問

面接の質問

配列の最初のいくつかの要素を配列の最後に移動することを、配列の回転と呼びます。減少しないソート済み配列の回転を入力し、回転した配列の最小要素を出力します。
たとえば、配列{3,4,5,1,2}は{1,2,3,4,5}の回転であり、配列の最小値は1です。

コード

public class Test10 {
    
    
	public static void main(String[] args) {
    
    
		int[] array = new int[] {
    
    1,2,4,3,5,6,0,-1,-100};
		int minNumberInRotateArray = minNumberInRotateArray(array );
		System.out.println(minNumberInRotateArray);
	}
	
	public static int minNumberInRotateArray(int [] array) {
    
    
	    if(array.length==0){
    
    
	            return 0;
	        }
	        for(int i=0;i<array.length-1;i++){
    
    
	            if(array[i] > array[i+1]){
    
    
	                return array[i+1];
	            }
	        }
	        return array[0];
	    }
}

5.フィボナッチシーケンスの問題

面接の質問

誰もがフィボナッチシーケンスを知っています。今度は整数nを入力する必要があります。フィボナッチシーケンスのn番目の項を出力してください。[科学]フィボナッチシーケンスは、0、1、1、2のようなシーケンスを指します。 3、5、8、13、21、34、55、89、144、233、377、610、987、1597、2584、4181、6765、10946、17711、28657、46368 ..

コード

public class Test11 {
    
    
	public static void main(String[] args) {
    
    
		int fibonacci = fibonacci(10);
		System.out.println(fibonacci);
	}
	
	public static int fibonacci(int n) {
    
    
			if (n<=0)
            return 0;
			
	        int a=1,b = 1;int temp;
	        for(int i=2;i<n;i++){
    
    
	            temp = a;
	            a = b;
	            b = temp + b;
	        }
	        return b;
	    }
}

6.カエルが階段を登る

面接の質問

カエルは一度に最大1ステップ、または最大2ステップジャンプできます。カエルがnレベルのステップでジャンプするジャンプ方法の総数を見つけます(異なる順序は異なる結果としてカウントされます)

コード

public class Test12 {
    
    
	public static void main(String[] args) {
    
    
		int jumpFloor = jumpFloor(18);
		System.out.println(jumpFloor);
	}
	
	public static int jumpFloor(int target) {
    
    
		  	if(target <= 0)
		            return 0;
		        if(target <= 2)
		            return target;
		        int a=1,b=2;
		        int temp;
		        for(int i=3;i<=target;i++){
    
    
		            temp = a;
		            a = b;
		            b += temp;
		        }
		        return b;
		    }
}

7.異常なカエルのジャンプの問題

インタビュー

カエルは一度に1ステップまたは2ステップまでジャンプできます... nステップまでジャンプすることもできます。カエルがnレベルのステップでジャンプするジャンプ方法の総数を見つけます。

コード

public class Test13 {
    
    
	public static void main(String[] args) {
    
    
		int jumpFloorII = jumpFloorII(18);
		System.out.println(jumpFloorII);
	}
	
	 public static int jumpFloorII(int target) {
    
    
	        if(target<=0)
	            return 0;
	        int sumPath = 0;
	        int path = 0;
	        for(int i=0;i<target;i++){
    
    
	            path = sumPath + 1;
	            sumPath = sumPath * 2 + 1;
	        }
	        return path;
	    }
}

8.長方形カバレッジの問題

面接の質問

2×1の小さな長方形を水平または垂直に使用して、大きな長方形をカバーできます。大きな2×nの長方形をn2 * 1の小さな長方形でオーバーラップせずにカバーする方法はいくつありますか?

コード

public class Test14 {
    
    

	public static void main(String[] args) {
    
    
		int rectCover = rectCover(10);
		System.out.println(rectCover);
	}
	
	public static int rectCover(int target) {
    
    
        if(target <= 0)
           return 0;
       if(target <= 2)
           return target;
       int a=1,b=2;
       int temp;
       for(int i=3;i<=target;i++){
    
    
           temp = a;
           a = b;
           b += temp;
       }
       return b;
   }

}

上記のアルゴリズムの質問はインタビューで非常に一般的であり、必須の質問です。学生がそれらを収集し、繰り返し練習することをお勧めします。

テクノロジーを学びたい友達は私の公式アカウントに注意を払うことができます:Java学習ガイド、テクノロジーグループへのプライベートメッセージ、私たちは一緒に学び、一緒に進歩します。

ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/xqnode/article/details/110914154