LeetCode解题报告--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.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
点击解题:Search Insert Position

分析:从给定排好顺序的数组,找出给定目标数字下标,存在则返回下标,不存在则返回目标数应该插入的位置下标。
提供两个可行思路:
1).二分查找
a.存在情况,就是经典二分查找问题
b.不存在情况,加个判别条件 nums[mid] < target && nums[mid + 1] > target 原因在于,如果存在target,则nums[mid]与target的关系必然存在三种 “>”,”<”和”==”。
2).双指针法
双指针法判断条件多,基本原理是left,rigth双指针分别指向nums首末元素,从左右两边分别开始判断元素(nums[left],nums[right] )与target的关系,则如果存在target,则nums[left(right)]与target的关系必然存在三种 “>”,”<”和”==”。以此为判断标准,就可找到index

至于暴力法也是可以求解,但会超时,我没试过,虽然题目没时间复杂度限制,但是应该是不允许。
盖提与Search for range思路相似,具体可参考该篇博客
改题是二分查找的变形题目,会二分查找,就迎刃而解。

Java代码 Accepted:
Binary Search:

public class Solution {
    public int searchInsert(int[] nums, int target) {

        //Binary Search
        int left = 0;
        int right = nums.length - 1;

        while(left <= right){
            int middle = (left + right) / 2;

            if(nums[middle] == target)
                return middle;
            else if(nums[middle] < target){
                left = middle + 1;
            }else if(nums[middle] > target){
                right = middle - 1;
            }else if(nums[middle] < target && nums[middle + 1] > target){
                return middle;
            }

        }
         return left;
    }
}

Double Points Search:

public class Solution {
     
    public int searchInsert(int[] nums, int target) {

        //double points search
        int left = 0;
        int right = nums.length - 1;
        //flag for whether nums[left] > target or nums[right] < target
        int flag = 0;
        //save index of target
        int index = 0;
        while(left <= right){
            //when nums's length == 1
            if(left == right){
                if(nums[left] > target)
                    return left;
                else if(nums[left] < target)
                    return left + 1;
                else
                    return left;
            }
            //find target,then save left to index
            else if(nums[left] == target){
                index = left;
                break;
            }
            //find target,then save right to index
            else if(nums[right] == target){
                index = right;
                break;
            }
            //when nums[left] < target, if nums[left + 1] > target, target not exsist
           else if(nums[left] < target){
                left ++;
                flag = 1;
            }
            else if(flag == 1 && nums[left] > target){
                index = left;
                break;
            }
            //when nums[right] > target, if nums[right - 1] < target, target not exsist
            else if(nums[right] > target){
                right --;
                flag = 1;
            }
            else if(flag == 1 && nums[right] < target){
                index = right;
                break;
            }
        }
        return index;
    }
}

Python 代码:
Binary Search:

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left = 0
        right = len(nums) - 1

        while(left <= right):
            mid = (left + right) / 2
            if(nums[mid] == target):
                return mid
            elif(nums[mid] < target):
                left = mid + 1
            elif(nums[mid] > target):
                right = mid - 1
            elif(nums[mid] < target and nums[mid + 1] < target):
                return mid + 1
        return left

你可能感兴趣的:(LeetCode解题报告,算法,python学习,java,LeetCode,解题报告,leetcode,java,python,算法,刷题)