Array
1. Two sum
1 | def twoSum(nums, target): |
11. Container With Most Water
1 | def maxArea(height): |
15. 3Sum
1 | def twoSum(target, nums): |
16. 3Sum Closest
1 | def threeSumClosest(nums, target): |
18. 4Sum
1 | def fourSum(nums, target): |
26. Remove Duplicates from Sorted Array
1 | def removeDuplicates(self, nums): |
27. Remove Element
1 | def removeElement(self, nums, val): |
next-permutation-algorithm.png
31. Next Permutation
1 | def nextPermutation(nums): |
34. Search for a Range
···python
def searchRange(nums, target):
lo, hi = 0, len(nums)-1
while lo <= hi:
mid = (hi + lo)//2
if nums[mid] == target:
while nums[lo] != target:
if nums[lo] < target:
lo += 1
while nums[hi] != target:
if nums[hi] > target:
hi -= 1
return [lo, hi]
elif nums[mid] < target:
lo = mid + 1
elif nums[mid] > target:
hi = mid - 1
return [-1, -1]
start = nums.index(target)
ncount = nums.count(target)
res = [start, start + ncount - 1]
return res
1 | ## 35. Search Insert Position |
39. Combination Sum
1 | def combinationSum(candidates, target): |
40. Combination Sum II
1 | def combinationSum2(candidates, target): |
48. Rotate Image
1 | def rotate(matrix): |
1 | def rotate(matrix): |
53. Maximum Subarray
1 | def maxSubArray(nums): |
54. Spiral Matrix
1 | def spiralOrder(matrix): |
55. Jump Game
1 | def canJump( nums): |
62. Unique Paths
1 | # def uniquePaths(self, m, n): |
66. Plus One
1 | def plusone(digits): |
当数组为[0,8,9], 结果为[9,0]
1 | def plusOne(digits): |
88. Merge Sorted Array
1 | def merge(self, nums1, m, nums2, n): |
118. Pascal’s Triangle
1 | def generate(numRows): |
119. Pascal’s Triangle II
1 | def getRow(rowIndex): |
121. Best Time to Buy and Sell Stock
1 | max = 0 |
1 | output = 0 |
169. Majority Element
1 | def majorityElement(self, nums): |
189. Rotate Array
1 | def rotate(self, nums, k): |
217. Contains Duplicate
1 | def containsDuplicate( nums): |
1 | def containsDuplicate(self, nums): |
219. Contains Duplicate II
1 | def containsNearbyDuplicate(nums, k): |
283. Move Zeroes
1 | def moveZeroes(nums): |