LeetCode T509 T16 T33 T34 T36 T41 T43 T48 T49

Insert picture description here

class Solution:
    def fib(self, n):
        if n==0:
            return 0
        if n==1:
            return 1
        else:
            return self.fib(n-1) + self.fib(n-2)

Insert picture description here

Insert picture description here

法一:暴力
class Solution:
    def threeSumClosest(self, nums, target):
        n = len(nums)
        if n==3:
            return sum(nums)

        # 三数和与目标数差值
        sumClosest = 10**4 + 10**3 + 1
        threeSum = 0
        for i in range(n):
            for j in range(i+1,n):
                for k in range(j+1,n):
                    threeS = nums[i] + nums[j] + nums[k]
                    if threeS == target:
                        return target
                    else:
                        temp = abs(target - threeS)
                        if temp < sumClosest:
                            threeSum = threeS
                            sumClosest = temp
        return threeSum

法二:(受四数和启发)
class Solution:
    def threeSumClosest(self, nums, target):
        n = len(nums)
        if n==3:
            return sum(nums)

        # 三数和与目标数差值
        sumClosest = 10**4 + 10**3 + 1
        towSum = dict()
        for i in range(n):
            for j in range(i+1,n):
                if (nums[i] + nums[j]) not in towSum.keys():
                    towSum[nums[i] + nums[j]] = [[i,j]]
                else:
                    towSum[nums[i] + nums[j]].append([i,j])
#         三数和
        ps = 0
#         遍历原始数据列表
        for k in range(n):
            for t in towSum:
                s = nums[k] + t
                # 如果存在,且三个数坐在列表位置不同,直接返回target
                if s == target:
                    for p in towSum[t]:
                        threeSet = set(p)
                        threeSet.add(k)
                        if len(threeSet) == 3:
                            return target
                # 如果不存在
                else:
                    # 找出三数与目标数最小差值,并返回三数
                    for p in towSum[t]:
                        threeSet = set(p)
                        threeSet.add(k)
                        if len(threeSet) == 3:
                            temp = abs(s - target)
                            if temp < sumClosest:
    #                             print(temp)
                                sumClosest = temp
                                ps = s

        return ps

Method 1:
Insert picture description here
Method 2:
Insert picture description here

Insert picture description here

# 法一
class Solution:
    def search(self, nums, target):
        for i,num in enumerate(nums):
            if num == target:
                return i
        return -1
        
 # 法二
class Solution:
    def search(self, nums, target):
        if target in nums:
            return nums.index(target)
        else:
            return -1       

Method 1:
Insert picture description here
Method 2:
Insert picture description here

Insert picture description here

class Solution:
    def searchRange(self, nums, target):
        n = len(nums)
        if target not in nums:
            return [-1,-1]
        result = []
        count = nums.index(target)
        result.append(count)
        if count+1 == n:
            result.append(count)
        for i in range(count+1,n):
            if nums[i] == target:
                if i == n-1:
                    result.append(i)
                continue
            else:
                result.append(i-1)
                break
        return result

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

class Solution:
    def isValidSudoku(self,board):
        for i in range(3):
            for j in range(3):
                tempList = []
                tempList += board[j*3][i*3:i*3+3]
                tempList += board[j*3 + 1][i*3:i*3+3]
                tempList += board[j*3 + 2][i*3:i*3+3]
                tempList = [t for t in tempList if t!='.']
                if len(tempList) != len(set(tempList)):
                    return False
        for i in range(9):
            tempList = [b for b in board[i] if b!='.']
            if len(tempList) != len(set(tempList)):
                return False
        for j in range(9):
            tempList = [b[j] for b in board if b[j]!='.']
            if len(tempList) != len(set(tempList)):
                return False
        return True

Insert picture description here

Insert picture description here

class Solution:
    def firstMissingPositive(self, nums):
        nums = sorted(nums)
        n = len(nums)
        count = 0
#         count_begin = 0
        for i in range(n):
            if nums[i] > 0:
                count = i
                break
        nums = nums[count:]
        
        # 去重后数组
        numsList = []
        # 去重操作
        for num in nums:
            if num not in numsList:
                numsList.append(num)
        n = len(numsList)
        for i in range(n):
            if numsList[i] != i+1:
                return i+1
            count = i+1
        return count + 1

Insert picture description here

Insert picture description here

class Solution:
    def multiply(self, num1, num2):
        return str(int(num1)*int(num2))

Insert picture description here

Insert picture description here
Insert picture description here

class Solution:
    def rotate(self, matrix):
        """
        Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        d = []
        for i in range(n):
            d.append([m for m in matrix[i]])
        for di in range(n):
            for j in range(n):
                matrix[j][n-1-di] = d[di][j]
        return matrix

Insert picture description here

Insert picture description here

class Solution:
    def groupAnagrams(self, strs):
        d = dict()
        n = len(strs)
        countList = [0]*n
        for s in strs:
            if s not in d.keys():
                d[s] = dict()
                countList[strs.index(s)] = 1
                for di in s:
                    if di not in d[s].keys():
                        d[s][di] = 1
                        
                    else:
                        d[s][di] += 1
            else:
                countList[strs.index(s)] += 1

        strsList = []
        
        # 去重
        for value in d.values():
            if value not in strsList:
                strsList.append(value)
        
        resultList = []
        count = 0
        for val in strsList:
            resultList.append([])
            for dd in d:
                if d[dd] == val:
                    for c in range(countList[strs.index(dd)]):
                        resultList[count].append(dd)
            count += 1
        
        return resultList

Insert picture description here

Guess you like

Origin blog.csdn.net/wjl__ai__/article/details/112175004