class Solution:
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l = len(nums)
i = 0
for count in range(l):
if target > nums[i]:
i += 1
count += 1
elif target < nums[i]:
nums.insert(i,target)
else:
nums.insert(i+1,target)
return i
java:
class Solution {
public int searchInsert(int[] nums, int target) {
int i; //不能在for循环内int,要在这里声明
if( nums.length==0){
return 0;
}
for(i=0;i<nums.length;i++){//for(int i...),这样的i在for循环内有效。return i时出错
if(target<=nums[i]){
break;
}
}
return i;
}
}