牛客网剑指offer在线编程题(更新中)

1.二维数组中的查找

题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
简单思路:先判断target是否在这一列,若在再进行查询,查询比下边方式更快的可用二分查找,感兴趣的可以自己尝试下二分查找。

public class Solution {
    public boolean Find(int target, int [][] array) {
        if(array.length == 0 || array[0].length == 0){
            return false;
        }
        for(int i = 0;i < array.length;i ++){
            if(target >= array[i][0] && target <= array[i][array[i].length - 1]){
                for(int j = 0; j < array[i].length; j ++){
                    if(target == array[i][j]){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

2.替换空格

题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
思路解析:此题用Java来实现是很简单的,一行代码即可str.toString().replaceAll(“\s”, “%20”);,这是考API的情况,出题者不一定想考这个,还有一种思路是用下边的代码实现的。

public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str == null || str.length() == 0){
            return new String(str);
        }
        char[] strToArray = str.toString().toCharArray();
        int count = 0;
        for (int i=0;i<strToArray.length;i++){
            if(strToArray[i] == ' '){
                count++;
            }
        }
        char[] result = new char[strToArray.length + count *2];
        int resIndex = 0;
        for(int i=0;i<strToArray.length;i++){
            if(strToArray[i] == ' '){
                result[resIndex++] = '%';
                result[resIndex++] = '2';
                result[resIndex++] = '0';
            }else{
                result[resIndex++] = strToArray[i];
            }
        }
        return new String(result);
    }
}

3.从尾到头打印链表

题目描述
输入一个链表,从尾到头打印链表每个节点的值。
思路解析:将listNode遍历到一个arraylist中,再反转到另一个list中。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        if(listNode == null){
            return list;
        }
        while(listNode != null){
            list.add(listNode.val);
            listNode = listNode.next;
        }
        for(int i = list.size()-1;i>=0;i--){
            list2.add(list.get(i));
        }
        return list2;
    }
}

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) {
        if (pre == null || pre.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(pre[0]);
        if (pre.length == 1) {
            root.left = null;
            root.right = null;
            return root;
        }
        int index = 0;
        for (int i = 0; i < in.length; i++) {
            if (in[i] == pre[0]) {
                index = i;
                break;
            }
        }
        if (index > 0) {// 左子树有值
            int[] preLeft = new int[index];
            int[] inLeft = new int[index];
            for (int i = 0; i < index; i++) {
                preLeft[i] = pre[i + 1];
                inLeft[i] = in[i];
            }
            root.left = reConstructBinaryTree(preLeft, inLeft);
        } else {
            root.left = null;
        }
        if (pre.length - 1 - index > 0) {// 右子树有值
            int[] preRight = new int[pre.length - 1 - index];
            int[] inRight = new int[pre.length - 1 - index];
            for (int i = 0; i < pre.length - 1 - index; i++) {
                preRight[i] = pre[index + i + 1];
                inRight[i] = in[index + i + 1];
            }
            root.right = reConstructBinaryTree(preRight, inRight);
        } else {
            root.right = null;
        }
        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.isEmpty()){
            while(!stack1.isEmpty()){
                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) {
        if(array.length == 0){
            return 0;
        }
        int min = array[0];
        for(int i =1;i<array.length;i++){
            if(array[i] < min){
                min = array[i];
                break;
            }
        }
        return min;
    }
}

7.菲波那切数列

题目描述:
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
n<=39
解题思路:若是使用递归,总是超内存,所以使用循环来实现。菲波那切数列:1,1,2,3,5,8,13,21…等等。写一个大小为3的数组,第一个数和第二个数分别为前边的数,第三个数为前两个数相加。

public class Solution {
    public int Fibonacci(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        int num[] = { 1, 1, 2 };
        for (int i = 3; i < n; i++) {
            num[0] = num[1];
            num[1] = num[2];
            num[2] = num[0] + num[1];
        }
        return num[2];
    }
}

8.跳台阶

题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
解题思路:其实就是一个菲薄那切数列,跳n阶,假设跳n阶台阶有f(n)中方法,则跳n-1阶台阶有f(n-1)中方法,跳n-2阶台阶有f(n-2)中方法。所以当第一次是跳1阶的时候,剩下的n-1阶有f(n-1)种方法;当第一次是跳2阶的时候,剩下的n-2阶有f(n-2)种方法,因此当跳n阶的时候总共有f(n-1)+f(n-2)中方法,当n=1时有1种方法,当n=2时有2中方法。

public class Solution {
    public int JumpFloor(int target) {
        if(target == 0){
            return 0;
        }
        if(target == 1 || target == 2){
            return target;
        }
        return JumpFloor(target - 1) + JumpFloor(target - 2);
    }
}

9.变态跳台阶

题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
解题思路:
也是递归的一种应用。假设跳n阶台阶有f(n)中方法,则跳n-1阶台阶有f(n-1)中方法,跳n-2阶台阶有f(n-2)中方法。当总共跳n阶的时候,第一次跳的时候有n中跳的方式,但是对应的当跳1阶的时候还剩f(n-1)中方式,对应的当跳2阶的时候还剩f(n-2)中方式,对应的当跳3阶的时候还剩f(n-3)中方式,依此类推…。因此f(n)=f(n-1)+f(n-2)+…+f(1),f(n-1)=f(n-2)+f(n-3)+…+f(1),由二式可得,f(n)=2*f(n-1)。

public class Solution {
    public int JumpFloorII(int target) {
         if(target == 0 || target == 1){
             return target;
         }
        return 2*JumpFloorII(target-1);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值