[Leetcode] 35. 搜索插入位置 Python3和java

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

 

你可能感兴趣的:(Leetcode)