leetcode-第35题-搜索插入位置

博主并没有什么算法基础,所以写的不好,勿喷,抛砖引玉,欢迎交流,感谢。 

//给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 
// 你可以假设数组中无重复元素。
// 示例 1:
// 输入: [1,3,5,6], 5
//输出: 2
// 示例 2:
// 输入: [1,3,5,6], 2
//输出: 1
// 示例 3:
// 输入: [1,3,5,6], 7
//输出: 4
// 示例 4:
// 输入: [1,3,5,6], 0
//输出: 0
// Related Topics 数组 二分查找
// 👍 676 👎 0
package com.zqh.leetcode.editor.cn;

//Java:搜索插入位置
public class P35SearchInsertPosition {
    public static void main(String[] args) {
        Solution solution = new P35SearchInsertPosition().new Solution();
//        System.out.println(solution.searchInsert(new int[]{1, 3}, 0));
        System.out.println(solution.searchInsert(new int[]{1, 3}, 2));
//        System.out.println(solution.searchInsert(new int[]{1, 3}, 4));
//        System.out.println(solution.searchInsert(new int[]{1, 3, 5, 6}, 0));
//        System.out.println(solution.searchInsert(new int[]{1, 3, 5, 6}, 5));
//        System.out.println(solution.searchInsert(new int[]{1, 3, 5, 6}, 2));
//        System.out.println(solution.searchInsert(new int[]{1, 3, 5, 6}, 7));
//        System.out.println(solution.searchInsert(new int[]{1, 2, 4, 6, 7}, 3));
//        System.out.println(solution.searchInsert(new int[]{3, 5, 7, 9, 10}, 8));
//        System.out.println(1 / 2);
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int searchInsert(int[] nums, int target) {
//            方案2:二分查找法
            return find(target, nums.length, nums);
//            方案1:循环判断
//            for (int i = 0; i < nums.length; i++) {
//                判断当前数是否大于目标值,大于说明在上一个索引
//                if (nums[i] > target) {
//                    return i == 0 ? 0 : i - 1;
//                }
//                如果是等于,就直接返回当前索引
//                if (nums[i] == target) {
//                    return i;
//                }
//                这里说面目标值大于所有数组值,就是最后的长度
//                if (i == nums.length - 1) {
//                    return nums.length;
//                }
//                这里说明是在中间  345那种
//                if (nums[i + 1] > target) {
//                    return i + 1;
//                }
//            }
//            return 0;
        }

        /**
         * 二分查找
         *
         * @param searchKey
         * @return
         */
        public int find(long searchKey, int maxArraySize, int[] array) {
            int lowerBound = 0;
            int upperBound = maxArraySize - 1;
            int curIndex;
            while (true) {
                //找出中间索引坐标
                curIndex = (lowerBound + upperBound) / 2;
                if (array[curIndex] == searchKey) {
                    //刚刚好相等就直接返回当前的坐标
                    return curIndex;
                }
                if (lowerBound > upperBound) {
                    //没有找到返回最大索引
                    if (curIndex == 0) {
                        //相同的时候,说明已经是处于最后的值了,但是被夹在中间,比如[1,3] 2
                        //第一次:index=(0+1)/2=0,array[0]=1<2,lowerBound=1
                        //第二次:index=(1+1)/2=1,array[1]=3>2,upperBound=0;
                        //第三次:index=(1+0)/2=0,1>0
                        return 1;
                    }
                    return curIndex + 1;
                }
                // 判断当前索引的值小于查找值的话
                if (array[curIndex] < searchKey) {
                    //太小了,开始的标记等于中间索引+1
                    lowerBound = curIndex + 1;
                } else {
                    //太大了
                    if (curIndex == 0) {
                        //相同的时候,说明已经是处于最后的值了,但是初始
                        return 0;
                    }
                    //结束的标记等于中间索引-1
                    upperBound = curIndex - 1;
                }
            }
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值