剑指offer经典66答案汇总(1)

目录

1 二维数组中的查找

2 替换空格

3 从尾到头打印链表

4 重建二叉树

5 用两个栈实现队列

6 旋转数组的最小值

7 牛客网斐波那契数列

8 牛客网跳台阶

9 牛客网变态跳台阶

10 矩形覆盖

11 牛客网二进制中1的个数

12 牛客网数值的整数次方

13 调整数组顺序使奇数位于偶数前面

14 链表中倒数第k个结点

15 反转链表

16 合并两个排序的列表

17 树的子结构

18 二叉树的镜像

19 顺时针打印矩阵

20 包含min函数的栈

21 栈的压入、弹出序列

22 从上往下打印二叉树

23 二叉搜索树的后序遍历序列

24 二叉树中和为某一值的路径

25 复杂链表的复制

26 二叉搜索树与双向链表

27 字符串的排列

28 数组中出现次数超过一半的数字

29 最小的K个数

30 连续子数组的最大和

31 整数中1出现的次数

32 把数组排成最小的数

33 丑数

 


二维数组中的查找

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

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

替换空格

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

public class Solution {
    public String replaceSpace(StringBuffer str) {
    	String str1 = str.toString();
        if(str1.equals(""))
            return str1;
        char [] strA = str1.toCharArray();
        int i = 0;
        int lengthSpace = 0;
        while(i < strA.length)
        {
            if(strA[i] == ' ')
                lengthSpace++;
            i++;
        }
        int newSL = strA.length + lengthSpace * 2;
        char [] newS = new char[newSL];
        int j = newSL - 1;
        i = strA.length - 1;
        while(i >= 0)
        {
            if(strA[i] != ' '){
                newS[j--] = strA[i--];
            }else{
                newS[j--] = '0';
                newS[j--] = '2';
                newS[j--] = '%';
                i--;
            }
        }
        return new String(newS);
    }
}

从尾到头打印链表

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        if(listNode == null)
            return arrayList;
        printListFromTailToHead(listNode, arrayList);
        return arrayList;
    }
    public void printListFromTailToHead(ListNode listNode, ArrayList<Integer> list){
        if(listNode.next != null){
            printListFromTailToHead(listNode.next, list);
        }
        list.add(listNode.val);
    }
}

重建二叉树

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

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        return BT(pre, 0, pre.length-1, in, 0, in.length-1);
    }
    public TreeNode BT(int[] pre, int startpre, int endpre, int[] in, int startin, int endin) {
        if ((startpre - endpre) > 0 || (startin - endin) > 0) {
            return null;
        }
        TreeNode root = new TreeNode(pre[startpre]);
        int rootorder = 0;
        for(int i = startin; i <= endin; i++) {
            if (in[i] == pre[startpre]) {
                rootorder = i;
            } else {
                System.out.println("error!");
            }
        }
        int leftlength = rootorder - startin;
        if (leftlength > 0) {
            root.left = BT(pre, startpre + 1, startpre + leftlength, in, startin, rootorder - 1);
        }
        if ((in.length - leftlength) > 0) {
            root.right = BT(pre, startpre + leftlength + 1, endpre, in, rootorder + 1, endin);
        }
        return root;
    }
}

用两个栈实现队列

用两个栈来实现一个队列,完成队列的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()) {
            while (stack1.empty() != true) {
                stack2.push(stack1.pop());
            }
        }
        if (stack2.empty() == true)
            return -1;
        return stack2.pop();
    }
}

旋转数组的最小值

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{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;
        if (array[0] < array[array.length-1])
            return array[0];
        int s = 0;
        int e = array.length - 1;
        int f = 0;
        while (s + 1 != e) {
            int m = (s + e) / 2;
            if (array[m] > array[s])
                s = m;
            else if (array[m] < array[e])
                e = m;
            else{
                s++;
            }
        }
        return array[e];
    }
}

牛客网斐波那契数列

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

public class Solution {
    public int Fibonacci(int n) {
        int a = 0;
        int b = 1;
        if(n == 0)
            return 0;
        if(n == 1)
            return 1;
        int i = 2;
        int sum = 0;
        while(i <= n){
            sum = a + b;
            a = b;
            b = sum;
            i++;
        }
        return sum;
    }
}

牛客网跳台阶

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

public class Solution {
    public int JumpFloor(int target) {
        if(target == 1)
            return 1;
        if(target == 2)
            return 2;
        int a = 1;
        int b = 2;
        int sum = a + b;
        for(int i=3;i<=target;i++)
        {
            sum = a + b;
            a = b;
            b = sum;
        }
        return sum;
    }
}

9 牛客网变态跳台阶

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

public class Solution {
    public int JumpFloorII(int target) {
        return (int)Math.pow(2, target - 1);
    }
}

10 矩形覆盖

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值