(Leetcode)力扣python刷题(一)

1.两数之和 

def twoSum( nums, target):
      
    for i in range(0,len(nums)):
        for j in range(i+1,len(nums)):
            if nums[i]+nums[j]==target:
                return [i,j]

'''
执行结果
twoSum([1,2,5,9,45,6,12],15)
[3, 5]
'''

2.找出现一次的数


def SingleNumber(nums):
    nums.sort()# 排序

    if (nums[0] != nums[1]):
        return nums[0]
    
    if (nums[len(nums)-1] != nums[len(nums)-2]):
        return nums[len(nums)-1],
    
    else:
        for i in range(1,len(nums)-1):
            if (nums[i]!=nums[i+1] and nums[i]!=nums[i-1]):
                return nums[i]

 

你可能感兴趣的:((Leetcode)力扣python刷题(一))