剑指offerJAVA版笔记(第七天、第三十七题至第四十二题)

37.数字在排序数组中出现的次数

  • 题目描述:统计一个数字在排序数组中出现的次数。
  • 解题思路:因为是有序数组故使用二分查找去做,写两个函数分别去取得第一个k和最后一个k的位置然后将两个位置相减即可得到结果。
public int GetNumberOfK(int[] array, int k) {
        if (array == null || array.length == 0)
            return 0;
        //找到第一个k的位置
        int first = getFirstK(array, k, 0, array.length - 1);
        //找到最后一个k的位置
        int last = getLastK(array, k, 0, array.length - 1);
        if (first == -1 || last == -1) {
            return 0;
        } else {
            return last - first + 1;
        }

    }

    //找到第一个k的位置
    public int getFirstK(int[] array, int k, int start, int end) {
        while (start <= end) {
            int mid = (start + end) / 2;
            if (k < array[mid])
                end = mid - 1;
            else if (k > array[mid])
                start = mid + 1;
            else {
                if ((mid > 0 && array[mid - 1] != k) || mid == 0)
                    return mid;
                else {
                    end = mid - 1;
                }
            }
        }
        return -1;
    }

    public int getLastK(int[] array, int k, int start, int end) {
        while (start <= end) {
            int mid = (start + end) / 2;
            if (k < array[mid])
                end = mid - 1;
            else if (k > array[mid])
                start = mid + 1;
            else {
                if ((mid < array.length - 1 && array[mid + 1] != k) || mid == array.length - 1)
                    return mid;
                else {
                    start = mid + 1;
                }
            }
        }
        return -1;
    }

38.二叉树的深度

  • 题目描述:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
  • 解题思路:1.层次遍历二叉树 2.递归遍历二叉树
public int TreeDepth1(TreeNode pRoot)
    {
        if(pRoot == null){
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(pRoot);
        // depth:当前节点所在的层数,count已经遍历了的节点数,nextCount下层的节点总数
        // 当count==nextCount的时候,代表本层的节点已经遍历完毕。
        int depth = 0, count = 0, nextCount = 1;
        while(queue.size()!=0){
            TreeNode top = queue.poll();
            count++;
            if(top.left != null){
                queue.add(top.left);
            }
            if(top.right != null){
                queue.add(top.right);
            }
            if(count == nextCount){
                nextCount = queue.size();
                count = 0;
                depth++;
            }
        }
        return depth;
    }
    //递归写法
    public int TreeDepth(TreeNode pRoot)
    {
        if(pRoot == null){
            return 0;
        }
        int left = TreeDepth(pRoot.left);
        int right = TreeDepth(pRoot.right);
        return Math.max(left, right) + 1;
    }

39.平衡二叉树

  • 题目描述:输入一棵二叉树,判断该二叉树是否是平衡二叉树。
 private boolean isBalanced = true;
    public boolean IsBalanced_Solution(TreeNode root) {

        getDepth(root);
        return isBalanced;
    }
    public int getDepth(TreeNode root) {
        if (root == null)
            return 0;
        int left = getDepth(root.left);
        int right = getDepth(root.right);

        if (Math.abs(left - right) > 1) {
            isBalanced = false;
        }
        return right > left ? right + 1 : left + 1;

    }

40.数组中只出现一次的数字

  • 题目描述:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
  • 解题思路:首先:位运算中异或的性质:两个相同数字异或=0,一个数和0异或还是它本身。当只有一个数出现一次时,我们把数组中所有的数,依次异或运算,最后剩下的就是落单的数,因为成对儿出现的都抵消了。依照这个思路,我们来看两个数(我们假设是AB)出现一次的数组。我们首先还是先异或,剩下的数字肯定是A、B异或的结果,这个结果的二进制中的1,表现的是A和B的不同的位。我们就取第一个1所在的位数,假设是第3位,接着把原数组分成两组,分组标准是第3位是否为1。如此,相同的数肯定在一个组,因为相同数字所有位都相同,而不同的数,肯定不在一组。然后把这两个组按照最开始的思路,依次异或,剩余的两个结果就是这两个只出现一次的数字。
 public void FindNumsAppearOnce(int[] array, int[] num1, int[] num2)    {
        int length = array.length;
        if(length == 2){
            num1[0] = array[0];
            num2[0] = array[1];
            return;
        }
        int bitResult = 0;
        for(int i = 0; i < length; ++i){
            bitResult ^= array[i];
        }
        int index = findFirst1(bitResult);
        for(int i = 0; i < length; ++i){
            if(isBit1(array[i], index)){
                num1[0] ^= array[i];
            }else{
                num2[0] ^= array[i];
            }
        }
    }

    private int findFirst1(int bitResult){
        int index = 0;
        while(((bitResult & 1) == 0) && index < 32){
            bitResult >>= 1;
            index++;
        }
        return index;
    }

    private boolean isBit1(int target, int index){
        return ((target >> index) & 1) == 1;
    }

41.和为S的连续正数序列

  • 题目描述:小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
  • 输出描述:输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
    //存放结果
        ArrayList<ArrayList<Integer> > result = new ArrayList<>();
        //两个起点,相当于动态窗口的两边,根据其窗口内的值的和来确定窗口的位置和大小
        int plow = 1,phigh = 2;
        while(phigh > plow){
            //由于是连续的,差为1的一个序列,那么求和公式是(a0+an)*n/2
            int cur = (phigh + plow) * (phigh - plow + 1) / 2;
            //相等,那么就将窗口范围的所有数添加进结果集
            if(cur == sum){
                ArrayList<Integer> list = new ArrayList<>();
                for(int i=plow;i<=phigh;i++){
                    list.add(i);
                }
                result.add(list);
                plow++;
                //如果当前窗口内的值之和小于sum,那么右边窗口右移一下
            }else if(cur < sum){
                phigh++;
            }else{
                //如果当前窗口内的值之和大于sum,那么左边窗口右移一下
                plow++;
            }
        }
        return result;

42.和为S的两个数字

  • 题目描述:输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
  • 输出描述:对应每个测试案例,输出两个数,小的先输出。
  public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
                ArrayList<Integer> list = new ArrayList<Integer>();
                if (array == null || array.length < 2) {
                    return list;
                }
                int i=0,j=array.length-1;
                while(i<j){
                    if(array[i]+array[j]==sum){
                        list.add(array[i]);
                        list.add(array[j]);
                        return list;
                    }else if(array[i]+array[j]>sum){
                        j--;
                    }else{
                        i++;
                    }

                }
                return list;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值