Python implementation of Leecode primary algorithm--array
#-*-Coding:utf-8-*-"" "@Created on 2018/6/3 17:06@author:zhifengfang" "" # Permutation array Delete duplicates def removeduplicates (nums): if Le N (nums) <= 1:return len (nums) i = 1 while len (nums)! = I:if Nums[i] = = Nums[i-1]: de L Nums[i] I-= 1 i + = 1 return len (nums) # best time to buy and sell shares 2def Maxprofit (prices): max = 0 If Len (prices ) <= 1:return 0 for I in range (len (prices)-1): If Prices[i] < Prices[i + 1]: max + = P Rices[i + 1]-prices[i] return max# rotation array def rotate (nums, k): # nums = nums[-k:] + nums[:k + 1] # print (nums) If Len (nums) > 1:k = k% len (nums) if k! = 0:temp = nums[-k:] Nums[k:] = Nums[:le N (nums)-K] nums[0:k] = temp print (nums) # Determine if there are duplicate elements in the array def containsduplicate (nums): # If Len (nums) >len (Set (Nums)): # return True # return False for num in nums:if nums.count (num) > 1:retur N True return false#Get numbers that appear only once in the Def Singlenumber (nums): numcounts = {} result = [] for num in nums:numcounts[num] = numcounts . Get (num, 0) + 1 for key in Numcounts.keys (): If Numcounts.get (key) = = 1:result.append (key) Break return result[0]# the intersection of two arrays iidef intersect (NUMS1, NUMS2): If Len (NUMS2) < Len (nums1): Nums1, NUMS2 = Nums2, Nums1 newnums = [] i = 0 while I < Len (nums1): j = 0 While J < Len (NUMS2): If nums1[i] = = Nums2[j]: newnums.append (Nums2[j]) del Nums1[i], nums2[j] I -= 1 J-= 1 Break J + = 1 i + = 1 return newnums# print (intersect ([9],[7 , 8,3,9,0,0,9,1,5])) # Plus 1def PlusOne (digits): Strdigits = "For example in Digits:strdigits + = str (example) strdigits = Int (strdigits) + 1 listdigits = [Int (str) for STR in STR (strdigits)] return listdigits# print (PlusOne ( [1, 2, 3]) # Mobile 0def MoveZeroes (Nums): # for I in range (len (nums)): i = 0 Zeroescount = 0 while i + Zeroescount < Len (nums): If nums[i] = = 0:nums[i:] = nums[i + 1:] + [0] I-= 1 Zeroescount + = 1 i + = 1 R Eturn nums# two number and Def twosum (Nums, target): D = {} for x in range (len (nums)): a = target-nums[x] if num S[x] in D:return d[nums[x]], x else:d[a] = Xnums = [3, 2, 4]target = 6# print (Twosum (Nums, Target)) def ISXT (STRs): Strset = set (STRs) for s in strset:if s! = ".": If Strs.count (s) > 1: Return False return true# valid Sudoku def isvalidsudoku (board): For I in range (9): Boardlie = [Exampl E[i] For example in board] key1 = Int (I/3) * 3 + 1 Key2 = 1 + (i% 3) * 3 Boardge = [board[key1-1 ][KEY2-1], Board[key1-1][key2], Board[key1-1][key2 + 1], board[key1][key2-1], Board[key1][key2], Board[key1][key2 + 1], Board[key1 + 1][key2-1], Board[key1 + 1][key2], Board[key1 + 1][key2 + 1]] if ISXT (board[i]) = = False:return False if ISXT (boardlie) = = False:return False if ISXT (boardge) = = Fals E:return False return trueboard = [[".", ".", "4", ".", ".", ".", "6", "3", "."], [".", ".", ".", ".", ".", " . ",". ",". ",". ",". ",". "], [" 5 ",". ",". ",". ",". ",". ",". "," 9 ",". "," ".", ".", ".", "5", "6", "." , ".", ".", "."], ["4", ".", "3", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".",. "."], [".", ".", ".", "5", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", [".", ".", ".", ".", ".", ".", ".", ".", "."]] # Print (Isvalidsudoku (board)) # Rotate Image def rotate (matrix): For I in range (len (matrix)): for J in Range (I+1,len (Matri x)): Matrix[i][j], matrix[j][i] = Matrix[j][i], Matrix[i][j] Matrix[i].reverse () Print (matrix) ma = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]rotate (MA)
Python implementations of the Leecode primary algorithm--arrays