简单题35-搜索插入位置(Java and Python)20240919
问题描述:
Java:
class Solution {
public int searchInsert(int[] nums, int target) {
int k = 0;
int i =0;
while(i<nums.length){
if(nums[i]==target){
return i;
}
if(nums[i]<=target){
k = i+1;
}
i++;
}
return k;
}
}
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
i = index = 0
while(i<len(nums)):
if(nums[i] == target):
return i
if(nums[i]<=target):
index = i+1
i +=1
return index