剑指Offer-68道面试题(1-10题)-Java实现

1. 二维数组中的查找 – 数据结构

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

public class Solution {
    public boolean Find(int target, int[][] array) {
        int rowLength = array.length;
        int colLength = array[0].length;
        int rowTemp = 0;
        int colTemp = colLength - 1;
        // 右上角开始搜
        while (rowTemp < rowLength && colTemp >= 0) {
            if (array[rowTemp][colTemp] == target) {
                return true;
            }
            if (array[rowTemp][colTemp] > target) {
                colTemp--;
                continue;
            }
            if (array[rowTemp][colTemp] < target) {
                rowTemp++;
                continue;
            }
        }
        return false;
    }
}
2. 替换空格 – 数据结构

题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

public class Solution {
    public String replaceSpace(StringBuffer str) {
        // String strtemp = new String(str);
        // strtemp = strtemp.replaceAll(" ","%20");
        String str2 = new String(str);
        char[] strtemp = str2.toCharArray();
        int strlength = strtemp.length;
        int countSpace = 0;
        for (int i = 0; i < strlength; i++) {
            if (strtemp[i] == ' ') {
                countSpace++;
            }
        }
        int strNoSpaceLength = strlength + countSpace * 2;
        char[] strNoSpace = new char[strNoSpaceLength];
        int index = strNoSpaceLength - 1;
        // 从后往前比较
        for (int i = strlength - 1; i >= 0; i--) {
            if (strtemp[i] == ' ') {
                strNoSpace[index--] = '0';
                strNoSpace[index--] = '2';
                strNoSpace[index--] = '%';
            } else {
                strNoSpace[index--] = strtemp[i];
            }
        }
        return String.valueOf(strNoSpace);
    }
}
3. 从尾到头打印链表 – 数据结构

题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        // 数据结构-栈
        Stack<Integer> stack = new Stack<Integer>();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        while (!stack.empty()) {
            list.add(stack.pop());
        }
        return list;
    }
}
4. 重建二叉树 – 数据结构

题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
        TreeNode root = preConstruct(pre, 0, pre.length - 1, in, 0,
                in.length - 1);
        return root;
    }
    
    public TreeNode preConstruct(int[] pre, int startPre, int endPre, int[] in,
            int startIn, int endIn) {
        if (startPre > endPre || startIn > endIn) {
            return null;
        }
        TreeNode root = new TreeNode(pre[startPre]);
        for (int i = startIn; i <= endIn; i++) {
            if (pre[startPre] == in[i]) {
                root.left = preConstruct(pre, startPre + 1, startPre
                        + (i - startIn), in, startIn, i - 1);
                root.right = preConstruct(pre, startPre + (i - startIn) + 1,
                        endPre, in, i + 1, endIn);
            }
        }
        return root;
    }
}
5. 用两个栈实现队列 – 数据结构

题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if (!stack2.empty()) {
            return stack2.pop();
        }
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
}
6. 旋转数组的最小数字 – 算法和数据操作

题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

import java.util.ArrayList;

public class Solution {
    public int minNumberInRotateArray(int[] array) {
        // 二分查找需要有序数组
        int arrayLength = array.length;
        if (array == null || arrayLength == 0) {
            return 0;
        }
        int min = array[0];
        for (int i = 0; i < arrayLength; i++) {
            if (min > array[i]) {
                min = array[i];
            }
        }
        return min;
    }
}
7. 斐波那契数列 – 算法和数据操作

题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39

public class Solution {
    public int Fibonacci(int n) {
        int a = 0;
        int b = 1;
        int num = 0;
        if (n == 0) {
            return n;
        }
        if (n == 1) {
            return n;
        }
        for (int i = 2; i <= n; i++) {
            num = a + b;
            a = b;
            b = num;
        }
        return num;
    }
}
8. 跳台阶 – 算法和数据操作

题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

public class Solution {
    public int JumpFloor(int target) {
        // 斐波那契数列
        if (target == 1 || target == 2) {
            return target;
        }
        int a = 1;
        int b = 2;
        int num = 0;
        for (int i = 3; i <= target; i++) {
            num = a + b;
            a = b;
            b = num;
        }
        return num;
    }
}
9. 变态跳台阶 – 算法和数据操作

题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

public class Solution {
    public int JumpFloorII(int target) {
        return (int) Math.pow(2, target - 1);
    }
}
10. 矩形覆盖 – 算法和数据操作

题目描述
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法?
比如n=3时,2
3的矩形块有3种覆盖方法。

public class Solution {
    public int RectCover(int target) {
        if (target == 1 || target == 2) {
            return target;
        }
        int a = 1;
        int b = 2;
        int num = 0;
        for (int i = 3; i <= target; i++) {
            num = a + b;
            a = b;
            b = num;
        }
        return num;
    }
}

其他题目

→→点击跳转

猜你喜欢

转载自blog.csdn.net/weixin_43845524/article/details/105730931
今日推荐