前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >35. Search Insert Position(二分法)

35. Search Insert Position(二分法)

作者头像
yesr
发布2019-03-14 12:59:06
2750
发布2019-03-14 12:59:06
举报
文章被收录于专栏:leetcode_solutionsleetcode_solutions

Search Insert Position

【题目】

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

(翻译:给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。)

Example 1:

代码语言:javascript
复制
Input: [1,3,5,6], 5
Output: 2

Example 2:

代码语言:javascript
复制
Input: [1,3,5,6], 2
Output: 1

Example 3:

代码语言:javascript
复制
Input: [1,3,5,6], 7
Output: 4

Example 4:

代码语言:javascript
复制
Input: [1,3,5,6], 0
Output: 0

【分析】

这道题其实是二分法的变形,我在这里写了四种不同的二分法来实现题目要求,其中前两种是同一个思路,后两种是同一个思路:

写法一:

代码语言:javascript
复制
public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length;
        while (start < end) {
            int mid = (start + end) >> 1;
            int midNum = nums[mid];
            if (midNum < target) {
                start = mid + 1;
            } else {
                end = mid;
            }
        }
        return start;
    }

写法二:

代码语言:javascript
复制
public int searchInsert1(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        int ans = nums.length;
        while (start <= end) {
            int mid = (start + end) >> 1;
            int midNum = nums[mid];

            if (midNum >= target) {
                ans = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return ans;
    }

写法三:

代码语言:javascript
复制
public int searchInsert2(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start + 1 < end) {
            int mid = (end - start) / 2 + start;
            if (target < nums[mid]) end = mid;
            else if (target > nums[mid]) start = mid;
            else return mid;
        }
        if (target <= nums[start]) {
            return start;
        } else if (target <= nums[end]) {
            return end;
        } else {
            return end + 1;
        }
    }

写法四:

代码语言:javascript
复制
public int searchInsert3(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            //int mid = (left + right)/2;
            int mid = (right - left) / 2 + left;
            if (nums[mid]==target) return mid;
            else if(nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }
        return left;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Search Insert Position
  • 【题目】
  • 【分析】
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档