classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: i = 0 while nums: num = nums[0] nums.pop(0) if target - num in nums: return [i, nums.index(target-num) + i + 1] i += 1
classSolution: deflongestCommonPrefix(self, strs: List[str]) -> str: prefix = "" if strs == []: return prefix else: n = min([len(x) for x in strs]) if n == 0: return prefix else: for i inrange(n): pre = [x[:i+1] for x in strs] iflen(set(pre)) == 1: prefix = strs[0][:i+1] if i == n-1: return prefix else: pass return prefix
-- Runtime=553ms CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN DECLARE M INT; SET M=N-1; RETURN ( # Write your MySQL query statement below. SELECT Salary FROM (SELECTDISTINCT Salary FROM Employee ORDERBY Salary DESC) a LIMIT 1OFFSET M ); END -- Runtime=416ms CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN DECLARE M INT; SET M=N-1; RETURN ( # Write your MySQL query statement below. SELECTDISTINCT Salary FROM Employee ORDERBY Salary DESC LIMIT 1OFFSET M );
classSolution: deflengthOfLIS(self, nums: List[int]) -> int: n = len(nums) ## 列表长度 if n == 0: return0 dp = [1for i inrange(n)] ## 初始值都是1 for i inrange(n): for j inrange(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) returnmax(dp)
## Runtime=68ms, Memory=13.9MB classSolution: defselfDividingNumbers(self, left: int, right: int) -> List[int]: result = [] for num inrange(left, right+1): nums = list(map(int, list(str(num)))) count = 0 if0in nums: pass else: for j in nums: if num%j == 0: count += 1 if count == len(nums): result.append(num) return result ## 参考 Solution ## Rumtime=36ms, Memory=14MB classSolution: defselfDividingNumbers(self, left: int, right: int) -> List[int]: result = [] for num inrange(left, right+1): for x instr(num): if x == "0"or num % int(x) != 0: break else: result.append(num) return result
## Runtime=56ms, Memory=13.9MB classSolution: defflipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: for i inrange(len(A)): A[i].reverse() A[i] = [1-x for x in A[i]] return A ## Runtime=52ms, Memory=14MB classSolution: defflipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: for i inrange(len(A)): A[i] = A[i][::-1] ## difference A[i] = [1-x for x in A[i]] return A ## Runtime=48ms, Memory=14MB classSolution: defflipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: for i, row inenumerate(A): ## differencee A[i] = row[::-1] for j, col inenumerate(A[i]): ## difference A[i][j] = 1 - col return A
## Runtime=84ms, Memory=15.1MB classSolution: defpeakIndexInMountainArray(self, A: List[int]) -> int: idx = int((len(A)-1)/2iflen(A)%2==1elselen(A)/2) left, right = idx-1, idx+1 while left>=0and right<len(A): if A[idx]>A[left] and A[idx]>A[right]: break elif A[idx]<A[left]: left -= 1 idx -= 1 right -= 1 else: left += 1 idx += 1 right += 1 return idx ## Runtime=80ms, Memory=15MB ## Binary search in Solution classSolution: defpeakIndexInMountainArray(self, A: List[int]) -> int: lo, hi = 0, len(A) - 1 while lo < hi: mi = int((lo + hi) / 2) if A[mi] < A[mi + 1]: lo = mi + 1 else: hi = mi return lo ## Runtime=84ms, Memory=15.2MB ## Linear scan classSolution: defpeakIndexInMountainArray(self, A: List[int]) -> int: for i inrange(len(A)): if A[i] > A[i+1]: return i
## 参考了 Solution classSolution: defdiStringMatch(self, S: str) -> List[int]: low, high = 0, len(S) result = [] for s in S: if s == 'I': result.append(low) low += 1 else: result.append(high) high -= 1 return result + [low]
## 参考了solutions in discuss classSolution: defremoveOuterParentheses(self, S: str) -> str: count, result = 0, [] for s in S: if s == ")": ## 完整配对count=0;增加一个")"count减一;增加一个"("count加一 count -= 1 if count > 0: result.append(s) if s == "(": count += 1 return"".join(result)
classSolution: defsubtractProductAndSum(self, n: int) -> int: num = [x for x instr(n)] num = list(map(int, num)) ## 将int拆开成单个数字列表 pp = 1 ss = 0 for nn in num: pp *= nn ss += nn return pp - ss
1290. Convert Binary Number in a Linked List to Integer
## Runtime=4720ms classSolution: defreplaceElements(self, arr: List[int]) -> List[int]: out = [] n = len(arr) for i inrange(n-1): out.append(max(arr[(i+1):])) out.append(-1) return out ## A solution in 'Discuss' ## Runtime=125ms classSolution: defreplaceElements(self, arr: List[int]) -> List[int]: max_num = -1 for i inrange(len(arr)-1, -1, -1): hold = arr[i] arr[i] = max_num if hold > max_num: max_num = hold return arr
classSolution: defsumZero(self, n: int) -> List[int]: result = [] if n % 2 == 0: lst = [x for x inrange(n-1)] result.extend(lst) result.append(-sum(lst)) else: lst = [x for x inrange(1,int((n+1)//2))] result.extend(lst) result.extend([-x for x in lst]) result.append(0) return result
1309. Decrypt String from Alphabet to Integer Mapping
classSolution: deffreqAlphabets(self, s: str) -> str: result = "" dic = {} for i inrange(97,123): if i <= 105: dic[str(i - 96)] = chr(i) else: dic[str(i - 96) + '#'] = chr(i) i = 0 while i < len(s): if i+2 < len(s) and s[i+2] == '#': key = s[i:i+3] i += 3 else: key = s[i] i += 1 result += dic[key] return result
classSolution: defdecompressRLElist(self, nums: List[int]) -> List[int]: n = len(nums) ans = [] for i inrange(int(n/2)): n = nums[2*i] num = nums[2*i+1] for j inrange(n): ans.append(num) return ans
classSolution: defmaximum69Number (self, num: int) -> int: i = 0 out = [x for x instr(num)] while i<len(out): if out[i] == '6': out[i] = '9' break i += 1 returnint(''.join(out))
classSolution: defnumberOfSteps (self, num: int) -> int: cou = 0 while num>0: if num % 2 == 0: ## if even num = int(num / 2) else: ## if odd num -= 1 cou += 1 return cou
classSolution: defcountNegatives(self, grid: List[List[int]]) -> int: nums = [] for i inrange(len(grid)): nums += grid[i] returnsum([1if x<0else0for x in nums])
## 参考了 solution in discuss classSolution: defsortString(self, s: str) -> str: result = [] s = [x for x in s] ## 等价于 s = list(s) direct = 1## =1表示从小到大;=-1表示从大到小 while s: ## 当s非空时 if direct == 1: ss = sorted(set(s)) ## 从小到大 else: ss = sorted(set(s))[::-1] ## 从大到小 result.extend(ss) for x in ss: s.remove(x) direct *= -1## 反向 return"".join(result)
1374. Generate a String With Characters That Have Odd Counts
classSolution: defluckyNumbers (self, matrix: List[List[int]]) -> List[int]: rows = [min(x) for x in matrix] n, m = len(matrix), len(matrix[0]) cols, ans = [], [] for j inrange(m): col = [x[j] for x in matrix] cols.append(max(col)) for i inrange(n): for j inrange(m): if matrix[i][j] == rows[i] and matrix[i][j] == cols[j]: ans.append(matrix[i][j]) return ans
classSolution: defcreateTargetArray(self, nums: List[int], index: List[int]) -> List[int]: ans = [] for i inrange(len(nums)): num, idx = nums[i], index[i] if idx >= len(ans): ans.append(num) else: a, b = ans[:idx], ans[idx:] ans = a + [num] + b return ans