Open azl397985856 opened 2 years ago
二分法查找,如果nums[mid]小于target,则答案一定不在mid及其右边,left=mid+1;如果nums[mid]大于target,则答案可能是mid,也可能是mid右边。答案的范围在[0,n],所以右指针指向n。
var searchInsert = function(nums, target) {
const n = nums.length;
let left = 0, right = n;
while(left < right){
let mid = (left + right) >> 1;
if(nums[mid] === target) return mid;
else if(nums[mid] < target) left = mid + 1;
else right = mid;
};
return left;
};
复杂度分析
// 解法一
var searchInsert = function (nums, target) {
let head = 0;
let tail = nums.length;
let mid = 0;
while (head < tail) {
mid = (head + tail) >> 1;
if (nums[mid] < target) head = mid + 1;
else {
tail = mid;
}
}
return head;
};
// 解法二
var searchInsert = function (nums, target) {
var i = 0;
while (i <= nums.length) {
if (nums[i] >= target) {
return i;
}
i++;
}
return nums.length;
};
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l = 0
r = len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return l
时间复杂度:O(logN)
空间复杂度:O(1)
二分
class Solution {
public int searchInsert(int[] nums, int target) {
//如果目标值不存在于数组中,返回它将会被按顺序插入的位置
//输入: nums = [1,3,5,6], target = 7
//输出: 4
int end=nums.length-1,start=0, mid=0;
while (start<=end){
mid=(end+start)/2;
if (nums[mid]==target){
return mid;
}
if (nums[mid]>target){
end=mid-1;
}else {
start=mid+1;
}
}
return start;
}
}
复杂度分析 时间复杂度: O(logn) 空间复杂度:O(1)
基础二分法
主要是判断两边的边界
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid;
} else {
left = mid + 1;
}
}
if (nums[left] < target) {
return left + 1;
} else {
return left;
}
}
}
var searchInsert = function(nums, target) {
let l = 0, r = nums.length - 1
while(l <= r) {
let m = parseInt((l + r) / 2)
if (nums[m] < target) {
l = m + 1
} else if (nums[m] > target) {
r = m - 1
} else {
return m
}
}
return l
};
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right + 1;
}
}
复杂度分析
class Solution {
public int searchInsert(int[] nums, int target) {
int s = 0;
int f = nums.length - 1;
int ans = nums.length;
//
while (s <= f) {
int b = (s + f) / 2;
if (nums[b] >= target) {
f = b-1;
ans = b;
} else {
s = b+1;
}
}
return ans;
}
}
二分法
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
r = len(nums)-1
l = 0
while l<=r:
mid = (r+l)//2
if target==nums[mid]:
return mid
elif target<nums[mid]:
r = mid-1
elif target>nums[mid]:
l = mid+1
return l
复杂度分析
https://leetcode-cn.com/problems/search-insert-position
看到 有序 log n 就自然想到二分法,一道常规的二分,而且 nums.length <= 10^4 直接左右边界相加也不会溢出
class Solution {
public int searchInsert(int[] nums, int target) {
// 排除边界
int n = nums.length;
if (nums[0] > target) return 0;
if (nums[n-1] < target) return n;
int left = 0, right = n-1;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] == target) return mid;
if (nums[mid] > target) right = mid;
else left = mid+1;
}
return left;
}
}
in place --> 2 pointer(slow: idx for insert, fast: walk idx )
Wrong thought: echange
not exchange, but replace!!!!!
eg:
# 0,1,2,1,1,2,2,3,3,
# s
# f
# s=f=1,(if == pass,) if != , nums[s] = nums[f] ,s+=1, f+=1 !not exchange, but replace!!!!!so could compare with previous one.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) <= 1:
return len(nums)
slow, fast = 1, 1
while fast <= len(nums) - 1:
# print(slow,fast, nums)
if nums[fast] != nums[fast - 1]:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
Time: O(n) Space:O(1)
二分法,需注意k不在数组内的情况
def fun(nums,k):
l=0
r=len(nums)-1
if k < nums[l]: #k小于数组第一个元素
nums.insert(l,k)
return l,nums
if k > nums[r]: #k大于数组最后一个元素
nums.append(k)
return r+1,nums
while l<=r:
mid=(l+r)//2
if k>nums[mid]:
l=mid+1
if k<nums[l]: #k不在数组内,位于相邻两个元素之间时
nums.insert(l,k)
return l,nums
elif k<nums[mid]:
r=mid
elif k == nums[mid]:
return mid,nums
nums=[1,5,6,8,9,13]
k=3
print(fun(nums,k)) #(1, [1, 3, 5, 6, 8, 9, 13])
二分查找
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0, right = nums.size();
while(right > left)
{
int mid = left + (right - left)/2;
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] < target)
{
left = mid + 1;
}
else if(nums[mid] > target)
{
right = mid;
}
}
return left;
}
};
## 复杂度
时间O(logn) 空间O(1)
二分
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function (nums, target) {
let left = 0, right = nums.length - 1;
while (left <= right) {
const mid = (left + right) >> 1;
if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = left + 1;
} else {
return mid;
}
}
return left;
};
时间:O(logN) 空间:O(1)
class Solution {
public int searchInsert(int[] nums, int target) {
int l = 0;
int r = nums.length-1;
while(l<=r){
int mid = l+((r-l)>>1);
if(nums[mid]>target){
r = mid-1;
}else if(nums[mid]<target){
l = mid+1;
}else{
return mid;
}
}
return r+1;
}
}
很基础的二分法,考虑一下边界情况即可
func searchInsert(nums []int, target int) int {
if len(nums) == 0{
return 0
}
if target <=nums[0]{
return 0
}
if target == nums[len(nums)-1]{
return len(nums)-1
}
if target > nums[len(nums)-1]{
return len(nums)
}
l:=0
r:=len(nums)
for l<r{
mid:= l+(r-l)/2
if nums[mid]<target&&nums[mid+1]>=target{
return mid+1
}
if nums[mid]<target{
l = mid
}else {
r = mid
}
}
return -1
}
时间:O(logn) 空间:O(1)
因为出现了复杂度O(logn)的要求,所以采用二分法是最为合适的。具体代码实现是基础的二分法。
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
int mid;
while(right >= left){
mid = left + (right - left) / 2;
if(nums[mid] < target) left = mid + 1;
else if(nums[mid] > target) right = mid - 1;
else{return mid;}
}
return left;
}
}
二分查找
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = (left + right) // 2
if target == nums[mid]:
return mid
elif target < nums[mid]:
right = mid - 1
else:
left = mid + 1
return left
二分
class Solution {
public int searchInsert(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l <= r) {
int mid = (l + r) >>> 1;
if (nums[mid] == target) {
return mid;
}
if (nums[mid] > target) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return l;
}
}
二分
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
l,r = 0,len(nums)
while l<r:
mid = (l+r)//2
if nums[mid] > target:
r = mid
elif nums[mid] == target:
return mid
else:
l = mid + 1
return l
时间复杂度:O(logn) 空间复杂度:O(1)
二分
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while(left<right){
int mid = (right-left)/2+left;
if(nums[mid] >= target) right = mid;
else left = mid+1;
}
return right;
}
};
复杂度分析
时间复杂度:O(n)
空间复杂度:O(1)
二分搜索
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if not nums or len(nums) == 0:
return 0
l, r = 0, len(nums)
while l < r :
mid = (l + r) // 2
if target > nums[mid]:
l = mid + 1
else:
r = mid
return l
TC: O(logN) SC: O(1)
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums)-1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid -1
return left
Time complexity O(logn) Space complexity O(n)
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (right + left) // 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
right = mid - 1
else:
left = mid + 1
return left
二分查找
public class Day27 {
/**
* 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
*
* 请必须使用时间复杂度为 O(log n) 的算法。
*
*/
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
while (left<=right){//=条件处理target大于最后一位
int mid = (left+right)/2;
if(nums[mid]<target){//中间值小于于 目标值
left = mid+1;
}else if(nums[mid]==target){
return mid;
}else{//中间值大于 目标值
right = mid-1;
}
}
return left;
}
}
时间复杂度:O(logN)
空间复杂度:O(1)
二分查找
最开始l
指向首位,r
指向末位,mid = (l + r) // 2
。nums[mid]
与target
比较,如果等于则返回mid
,小于则将l = mid + 1
,大于则将r = mid - 1
,直到l == r
时终止即未找到返回l
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n = len(nums)
l, r = 0 , n - 1
while l <= r:
mid = (r + l) >> 1
if target == nums[mid]:
return mid
elif target < nums[mid]:
r = mid - 1
else:
l = mid + 1
return l
C++ Code:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int start = 0, end = nums.size() - 1;
int mid = 0;
while (start <= end) {
mid = start + (end - start) / 2;
if (nums[mid] < target) {
start = mid + 1;
} else if (nums[mid] > target) {
end = mid - 1;
} else if (nums[mid] == target) {
return mid;
}
}
return start;
}
};
复杂度分析
class Solution: def searchInsert(self, nums: List[int], target: int) -> int: l, r = 0, len(nums)-1 if nums[0] > target: return 0 if nums[-1] < target: return len(nums) while l <= r: mid = (l+r)//2 if nums[mid] == target: return mid elif nums[mid] > target: if mid > 0 and nums[mid-1] < target: return mid r = mid - 1 else: if mid < len(nums)-1 and nums[mid+1] > target: return mid + 1 l = mid + 1
双指针
class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length == 0) return 0;
int left = 0, right = nums.length - 1;
if (target > nums[nums.length - 1]) return nums.length;
if (target <= nums[0]) return 0;
while (left < right) {
int mid = (left + right) / 2;
if (target > nums[mid]) {
left = mid;
} else if (target < nums[mid]) {
right = mid;
} else {
return mid;
}
if ((left + 1) == right) break;
}
return right;
}
}
class Solution { public int searchInsert(int[] nums, int target) { int temp=0; for(int i=0; i<nums.length; i++){ if(nums[i]<target){ temp =i+1; }else if(nums[i] == target){ return i; }else{ break; } } return temp; } }
https://leetcode.com/problems/search-insert-position/
class Solution {
public int searchInsert(int[] nums, int target) {
if(nums == null || nums.length == 0){
return 0;
}
int start = 0;
int end = nums.length - 1;
while(start + 1 < end){
int mid = start + (end - start) / 2;
if(target < nums[mid]){
end = mid;
}else if(target > nums[mid]){
start = mid;
}else{
start = mid;
}
}
if(target <= nums[start]){
return start;
}else if(target <= nums[end]){
return end;
}else{
return end + 1;
}
}
}
二分
var searchInsert = function(nums, target) {
let left = 0
let right = nums.length
while(left < right) {
let mid = left + Math.floor((right - left) / 2)
if (nums[mid] >= target) {
right = mid
}
if (nums[mid] < target) {
left = mid +1
}
}
return left
}
时间复杂度:O(logn)
空间复杂度:O(1)
思路
代码
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
l, r = 0, len(nums)-1
while l <= r:
mid = (l+r) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid+1
else:
r = mid-1
return l
复杂度分析
二分,需要小心r==l+1的情况
class Solution {
public int searchInsert(int[] nums, int target) {
if(target > nums[nums.length-1]) return nums.length;
if(target < nums[0]) return 0;
int l = 0, r = nums.length - 1;
int mid = (l + r) / 2;
while (nums[mid]!= target) {
if(r==l+1) {
mid = mid+1;
break;
};
if (nums[mid] > target) {
r = mid;
} else {
l = mid;
}
mid = (l + r) / 2;
}
return mid;
}
}
时间复杂度 O(logN) 空间复杂度 O(1)
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
return len([ x for x in nums if x < target ])
双指针
Python3 Code:
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
length = len(nums)
if target > nums[-1]: return length
left = 0
right = length-1
while left <= right:
mid = (right+left)//2
if nums[mid]>target:
right = mid - 1
elif nums[mid]<target:
left = mid + 1
else:
return mid
return left
复杂度分析
令 n 为数组长度。
1.二分,没啥好说的
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function (nums, target) {
if (target > nums[nums.length - 1]) {
return nums.length
}
if (target < nums[0]) {
return 0
}
let start = 0;
let end = nums.length - 1
while (start < end) {
let mid = Math.floor((start + end) / 2);
let num = nums[mid];
if (num === target) {
return mid;
} else if (num > target) {
end = mid - 1
} else {
start = mid + 1
}
}
return target <= nums[start] ? start : start + 1;
};
复杂度分析不是很会,不一定对,如果有错,请指正。
二分
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
left = mid + 1;
} else if(nums[mid] > target){
right = mid - 1;
}
}
return left;
}
}
时间复杂度 O(logn) 空间复杂度 O(1)
code
public int searchInsert(int[] nums, int target) {
if(nums==null || nums.length==0) return 0;
int low = 0, high = nums.length-1;
while(low <= high){
int mid = (low + high)/2;
if(nums[mid] == target){
return mid;
}
else if(nums[mid] > target){
high = mid - 1;
}
else{
low = mid + 1 ;
}
}
return low;
}
var searchInsert = function(nums, target) {
let i = 0;
let n = nums.length-1;
let res = nums.length;
while(i<=n){
const mid = Math.floor((n+i)/2);
if(nums[mid]>=target){
res = mid;
n=mid-1;
}else{
i=mid+1;
}
}
return res;
};
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
l = 0
r = len(nums)-1
while(l <= r):
mid = l + ((r-l)>>1) #右移运算优先级小于加法,要加括号
print(mid)
if nums[mid] == target:
return mid
elif nums[mid] > target:
r = mid-1
else:
l = mid+1
return l
time: log(n) space:1
class Solution { public int searchInsert(int[] nums, int target) { int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
return mid;
}
}
return left;
}
}
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
low = 0
high = len(nums) - 1
while(low<=high):
mid = (low+high)/2
if(nums[mid]==target):
return mid
if(nums[mid]>target):
high = mid - 1
if(nums[mid]<target):
low = mid + 1
return low
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-left)//2) if nums[mid] ==target: return mid elif target>nums[mid]: left=mid+1 else: right=mid-1 return left
https://leetcode-cn.com/problems/search-insert-position/
二分法
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0, right = nums.size();
while(left<right)
{
int mid = left+((right-left)/2);
if(nums[mid]>target)
{
right = mid;
}
else if(nums[mid]<target)
{
left = mid+1;
}
else
{
return mid;
}
}
return right;
}
};
复杂度分析
var searchInsert = function(nums, target) {
const n = nums.length;
let left = 0, right = n - 1, ans = n;
while (left <= right) {
let mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
};
思路 二分
class Solution {
public:
int searchInsert(vector
while (l <= r) {
int mid = l + (r - l)/2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) l = mid + 1;
else
r = mid - 1;
}
return l;
}
};
时间复杂度: O(logn) 空间复杂度: O(1)
class Solution:
def searchInsert(self, nums: List[int], target: int) -> 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
return left
二分查找
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int i=0,j=nums.size()-1;
while(i<=j){
int mid=i+(j-i)/2;
if(nums[mid]==target) return mid;
if(nums[mid]>target){
j=mid-1;
}else{
i=mid+1;
}
}
return i;
}
};
时间复杂度:O(logn)
空间复杂度:O(1)
二分法
// 二分法
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while(left < right) {
int middle = left + (right - left) / 2;
if (nums[middle] >= target) {
right = middle;
} else {
left = middle + 1;
}
}
return left;
}
};
35. 搜索插入位置
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/search-insert-position
前置知识
暂无
题目描述
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。