JAVA程序员的必杀技:8个经典算法题,过程精妙,代码精炼

总有一些题,超越了岁月,即便是经过了新框架的层层迭代,它依然散发着令人回味无穷的味道。下面的几个笔试题目,是JAVA面试中经常遇见的,大家一定要牢记于心,可别复习到了到时候又说不出来。我就吃过这种亏,不说啦,下面来看题目。

二维数组中的查找

  • 面试题

在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

  • @代码

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;
    }

}

链表题

  • 面试题

输入一个链表,按链表从尾到头的顺序返回一个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;
        }

}

队列题

  • 面试题

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为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;
    }}

数组题

  • 面试题

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组 {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];
        }
}

斐波那契数列问题

  • 面试题

大家都知道斐波那契数列,现在要求输入一个整数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;
        }
}

青蛙上台阶问题

  • 面试题

一只青蛙一次可以跳上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;
            }
}

变态青蛙跳台阶问题

  • 面试

一只青蛙一次可以跳上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;
        }
}

矩形覆盖问题

  • 面试题

我们可以用 2×1 的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2×n的大矩形,总共有多少种方法?

  • 代码

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;
   }

}

这些题目虽然看上去比较简单,但是蕴含了丰富的编程思想,大家可以经常练习,提高思维能力。这是面试题中经常出现的,还有一些类似的题目,大家有的话也可以提出来,我们一起进步!

作者: 爱吃早餐的程序员
原文链接:https://blog.csdn.net/weixin_39076203/article/details/110404021

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章