LeetCode Python 解法

前言

刷刷简单的算法问题,顺手提升一下编程的裸写代码的能力。

本文为 LeetCode 的 Easy 算法的解法集合。

Easy 集合

1. Two Sum 29.4%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 尝试:暴力解决
# 结果:超时
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
begin = 0
end = 0
for i in range(len(nums)):
for j in range(i,len(nums)):
if i != j and target == nums[i] + nums[j]:
return [i,j]
# 尝试:字典也就是 HashMap 解决
# 结果:读题不准确,数组不行
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
nums_dict = {}
for i,num in enumerate(nums):
nums_dict[num] = i
for num in nums:
item_num_1st = nums_dict.get(num)
item_num_2st = nums_dict.get(target-num)
if item_num_2st is not None and item_num_1st != item_num_2st:
return [item_num_1st,item_num_2st]
# 尝试:其他
# 结果:其他

6. ZigZag Conversion 25.8%

7 Reverse Integer 23.7% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
preffix = ""
if "-" in str(x):
num = int(str(x).strip("-")[::-1])
preffix = "-"
else:
num = int(str(x)[::-1])
if num >= 2147483648 or num <= -2147483647:
return 0
return int(preffix+str(num))

8 String to Integer (atoi) 13.8% Easy

9 Palindrome Number 34.0% Easy

1
2
3
4
5
6
7
8
9
# 使用了额外的存储,亟待优化
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
l_x = list(str(x))
return l_x == l_x[::-1]

13 Roman to Integer 43.2% Easy

14 Longest Common Prefix 30.4% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ""
current_clip = ""
len_strs = [len(_str) for _str in strs]
min_strs_len = min(len_strs)
has_common_flag = False
for _len in range(min_strs_len,0,-1):
cliped_strs_set = set()
for _str in strs:
current_clip = _str[0:_len]
cliped_strs_set.add(current_clip)
if len(cliped_strs_set) == 1:
has_common_flag = True
break
if has_common_flag:
return current_clip
else:
return ""

19 Remove Nth Node From End of List 32.0% Easy

20 Valid Parentheses 31.9% Easy

21 Merge Two Sorted Lists 37.7% Easy

24 Swap Nodes in Pairs 37.0% Easy

26 Remove Duplicates from Sorted Array 35.2% Easy

27 Remove Element 36.7% Easy

28 Implement strStr() 26.8% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
cur = -1
try:
cur = haystack.index(needle)
except Exception:
pass
return cur

36 Valid Sudoku 33.7% Easy

1
2
3
4
5
6
7
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# 横向

38 Count and Say 32.3% Easy

58 Length of Last Word 31.0% Easy

66 Plus One 36.7% Easy

67 Add Binary 30.2% Easy

70 Climbing Stairs 38.5% Easy

83 Remove Duplicates from Sorted List 38.7% Easy

88 Merge Sorted Array 31.3% Easy

100 Same Tree 45.1% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if p is None and q is None:
return True
elif p is not None and q is not None:
return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
else:
return False

101 Symmetric Tree 36.7% Easy

102 Binary Tree Level Order Traversal 36.7% Easy

104 Maximum Depth of Binary Tree 50.6% Easy

107 Binary Tree Level Order Traversal II 37.5% Easy

110 Balanced Binary Tree 36.1% Easy

111 Minimum Depth of Binary Tree 32.2% Easy

112 Path Sum 32.8% Easy

118 Pascal’s Triangle 36.5% Easy

119 Pascal’s Triangle II 34.8% Easy

121 Best Time to Buy and Sell Stock 38.9% Easy

125 Valid Palindrome 25.2% Easy

136 Single Number 52.6% Easy

141 Linked List Cycle 35.9% Easy

155 Min Stack 26.0% Easy

157 Read N Characters Given Read4 29.4% Easy

160 Intersection of Two Linked Lists 30.0% Easy

165 Compare Version Numbers 19.1% Easy

168 Excel Sheet Column Title 24.2% Easy

1
2
3
4
5
6
7
8
9
10
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
for i,c in enumerate(s[::-1]):
result += (ord(c) - ord("A") + 1) * (26 ** i)
return result

169 Majority Element 44.6% Easy

1
2
3
4
5
6
7
8
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
from collections import Counter
return Counter(nums).most_common()[0][0]

170 Two Sum III - Data structure design 22.6% Easy

171 Excel Sheet Column Number 45.0% Easy

172 Factorial Trailing Zeroes 34.7% Easy

189 Rotate Array 23.3% Easy

190 Reverse Bits 29.5% Easy

1
2
3
4
5
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
return int(bin(n).replace("0b","").zfill(32)[::-1],2)

191 Number of 1 Bits 38.4% Easy

198 House Robber 37.2% Easy

202 Happy Number 39.0% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
return self.isHappyNum(self,n,[])
def isHappyNum(self,n,result_list):
result = reduce(lambda x , y : x+ y,map(lambda x : int(x) ** 2 ,str(n).split()))
if result in result_list:
return False
else:
result_list.append(result)
self.isHappyNum(result,result_list)

203 Remove Linked List Elements 30.8% Easy

204 Count Primes 26.0% Easy

205 Isomorphic Strings 32.3% Easy

206 Reverse Linked List 43.2% Easy

217 Contains Duplicate 43.5% Easy

1
2
3
4
5
6
7
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return nums is not None and len(nums) >= 1 and len(nums) > len(set(nums))

219 Contains Duplicate II 31.3% Easy

223 Rectangle Area 31.8% Easy

225 Implement Stack using Queues 31.0% Easy

226 Invert Binary Tree 49.5% Easy

231 Power of Two 39.1% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n % 2 == 1 and n > 1:
return False
elif n <= 0:
return False
elif n in (2,1):
return True
else:
return self.isPowerOfTwo(n / 2)

232 Implement Queue using Stacks 34.9% Easy

234 Palindrome Linked List 31.4% Easy

235 Lowest Common Ancestor of a Binary Search Tree 38.0% Easy

237 Delete Node in a Linked List 45.2% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
这个方法是可以 Get 到 node 点的,但是题目的要求应该不是这个意思
"""
if node.next is not None:
node = node.next

242 Valid Anagram 44.6% Easy

1
2
3
4
5
6
7
8
9
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
from collections import Counter
return Counter(s) == Counter(t)

243 Shortest Word Distance 50.5% Easy

246 Strobogrammatic Number 38.7% Easy

249 Group Shifted Strings 38.1% Easy

252 Meeting Rooms 45.2% Easy

257 Binary Tree Paths 34.3% Easy

258 Add Digits 50.1% Easy

263 Ugly Number 38.3% Easy

266 Palindrome Permutation 54.8% Easy

270 Closest Binary Search Tree Value 37.8% Easy

276 Paint Fence 33.7% Easy

278 First Bad Version 24.2% Easy

283 Move Zeroes 47.5% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 首先想到的这个答案
# 然后 leetcode 上面居然还是打印的原来的值,我猜测肯定是用后台进行 id 比对内存地址。好吧,这个方式最好。
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
zero_count = nums.count(0)
new_nums = [num for num in nums if num != 0]
new_nums.extend([0]*zero_count)
nums = new_nums
## 蛋疼,三重排序
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
begin_cur = 0
end_cur = len(nums) - 1
for i in range(len(nums)):
for j in range(i,len(nums)):
if nums[j] == 0 and nums[j+1] != 0:
nums[j] , nums[j+1] = nums[j+1] , nums[j]
print(nums)

288 Unique Word Abbreviation 15.3% Easy

290 Word Pattern 31.9% Easy

292 Nim Game 54.6% Easy

293 Flip Game 53.7% Easy

299 Bulls and Cows 33.0% Easy

303 Range Sum Query - Immutable 26.1% Easy

326 Power of Three 39.1% Easy

339 Nested List Weight Sum 59.5% Easy

342 Power of Four 37.2% Easy

344 Reverse String 57.4% Easy

1
2
3
4
5
6
7
class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]

345 Reverse Vowels of a String 37.1% Easy

346 Moving Average from Data Stream 57.1% Easy

349 Intersection of Two Arrays 45.4% Easy

1
2
3
4
5
6
7
8
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list(set(nums1) & set(nums2))

350 Intersection of Two Arrays II 43.4% Easy

359 Logger Rate Limiter 58.0% Easy

371 Sum of Two Integers 51.5% Easy

374 Guess Number Higher or Lower 33.2% Easy

383 Ransom Note 45.9% Easy

387 First Unique Character in a String 45.1% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
flag = -1
from collections import Counter
uniq_chars_set = set([k for k , v in Counter(s).items() if v == 1])
for i,c in enumerate(s):
if c in uniq_chars_set:
return i
return flag

389 Find the Difference 50.3% Easy

396 Rotate Function 30.1% Easy

400 Nth Digit 30.6% Easy

401 Binary Watch 43.1% Easy

404 Sum of Left Leaves 45.6% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
return self.newSumOfLeftLeaves(root.left,-1) + self.newSumOfLeftLeaves(root.right,1)
def newSumOfLeftLeaves(self,root,flag):
if root is None:
return 0
elif root.left is not None and root.right is not None:
return self.newSumOfLeftLeaves(root.left,-1) + self.newSumOfLeftLeaves(root.right,1)
elif root.left is None and root.right is None:
if flag == -1:
return root.val + self.newSumOfLeftLeaves(root.right,1)
else:
return 0
elif root.left is None and root.right is not None:
return self.newSumOfLeftLeaves(root.right,1)
elif root.left is not None and root.right is None:
return self.newSumOfLeftLeaves(root.left,-1)

405 Convert a Number to Hexadecimal 40.6% Easy

408 Valid Word Abbreviation 27.2% Easy

409 Longest Palindrome 44.1% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
from collections import Counter
max_len = 0
odd_count = 0
for _chr,_chrs_len in Counter(s).items():
if _chrs_len % 2 == 1:
odd_count = 1
max_len += _chrs_len - 1
else:
max_len += _chrs_len
return max_len + odd_count

412 Fizz Buzz 58.0% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
arr = []
for i in range(1,n+1):
item = None
if i % 3 == 0 and i % 5 == 0:
item = "FizzBuzz"
arr.append(item)
continue
if i % 3 == 0:
item = "Fizz"
arr.append(item)
continue
if i % 5 == 0:
item = "Buzz"
arr.append(item)
continue
arr.append(str(i))
return arr

414 Third Maximum Number 26.7% Easy

415 Add Strings 41.1% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 哼,让我不用,我偏偏要用转 int
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
return str(int(num1) + int(num2))
# 我就不用 int 都可以 one line
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
return str(eval("{} + {}".format(num1,num2)))

422 Valid Word Square 36.2% Easy

434 Number of Segments in a String 39.0% Easy

437 Path Sum III 38.6% Easy

438 Find All Anagrams in a String 33.4% Easy

441 Arranging Coins 36.2% Easy

447 Number of Boomerangs 41.8% Easy

448 Find All Numbers Disappeared in an Array 61.3% Easy

1
2
3
4
5
6
7
8
9
10
11
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
len_nums = len(nums)
if len_nums < 1:
return []
l = list(set(range(1,len_nums + 1)) - set(nums))
return l

453 Minimum Moves to Equal Array Elements 45.9% Easy

1
2
3
4
5
6
7
8
class Solution(object):
def minMoves(self, nums):
"""
:type nums: List[int]
:rtype: int
n - 1 个元素 ++ 等同于 1 个元素 --
"""
return sum(nums) - len(nums) * min(nums)

455 Assign Cookies 48.7% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 二叉树?
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
if len(g) == 0 or len(s) == 0:
return 0
g.sort()
s.sort()
satisfied_count = 0
while len(s) > 0:
max_cookie = s.pop()
while len(g) > 0:
max_child = g.pop()
if max_child <= max_cookie:
satisfied_count += 1
break
return satisfied_count

459 Repeated Substring Pattern 39.6% Easy

461 Hamming Distance 74.8% Easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
ma = str(bin(max(x,y))).replace("0b","").zfill(31)
ma_len = len(ma)
mi = str(bin(min(x,y))).replace("0b","").zfill(31)
print(mi)
print(ma)
count = 0;
for i, chr in enumerate(mi):
if ma[i] != chr:
count += 1
return count

463 Island Perimeter 56.3% Easy

475 Heaters 30.3% Easy

566. Reshape the Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
row_len = len(nums)
col_len = len(nums[0])
if row_len * col_len != r * c:
return nums
else:
new_nums = [[0 for i in range(c)] for j in range(r)]
cur = 0
for row_nu in range(row_len):
for col_nu in range(col_len):
cur += 1
new_row_nu = (cur - 1) // c
new_col_nu = cur - new_row_nu * c - 1
new_nums[new_row_nu][new_col_nu] = nums[row_nu][col_nu]
return new_nums

Medium 集合

2 Add Two Numbers 26.1% Medium

3 Longest Substring Without Repeating Characters 23.6% Medium

5 Longest Palindromic Substring 24.4% Medium

11 Container With Most Water 36.0% Medium

12 Integer to Roman 42.6% Medium

15 3Sum 20.7% Medium

16 3Sum Closest 30.4% Medium

17 Letter Combinations of a Phone Number 32.1% Medium

18 4Sum 25.5% Medium

22 Generate Parentheses 41.5% Medium

29 Divide Two Integers 15.9% Medium

31 Next Permutation 28.0% Medium

34 Search for a Range 30.8% Medium

35 Search Insert Position 38.8% Medium

39 Combination Sum 35.4% Medium

40 Combination Sum II 31.0% Medium

43 Multiply Strings 25.8% Medium

46 Permutations 40.1% Medium

47 Permutations II 30.8% Medium

48 Rotate Image 36.9% Medium

49 Group Anagrams 31.5% Medium

50 Pow(x, n) 27.1% Medium

53 Maximum Subarray 38.6% Medium

54 Spiral Matrix 24.4% Medium

55 Jump Game 29.1% Medium

59 Spiral Matrix II 37.8% Medium

60 Permutation Sequence 27.0% Medium

61 Rotate List 24.0% Medium

62 Unique Paths 39.0% Medium

63 Unique Paths II 30.8% Medium

64 Minimum Path Sum 37.0% Medium

69 Sqrt(x) 26.8% Medium

71 Simplify Path 23.9% Medium

73 Set Matrix Zeroes 35.0% Medium

74 Search a 2D Matrix 35.6% Medium

75 Sort Colors 36.5% Medium

77 Combinations 37.6% Medium

78 Subsets 36.2% Medium

79 Word Search 25.2% Medium

80 Remove Duplicates from Sorted Array II 34.7% Medium

81 Search in Rotated Sorted Array II 32.9% Medium

82 Remove Duplicates from Sorted List II 28.5% Medium

86 Partition List 31.4% Medium

89 Gray Code 39.1% Medium

90 Subsets II 33.6% Medium

91 Decode Ways 18.8% Medium

92 Reverse Linked List II 29.7% Medium

93 Restore IP Addresses 25.7% Medium

94 Binary Tree Inorder Traversal 43.5% Medium

95 Unique Binary Search Trees II 30.5% Medium

96 Unique Binary Search Trees 39.6% Medium

98 Validate Binary Search Tree 22.2% Medium

103 Binary Tree Zigzag Level Order Traversal 32.0% Medium

105 Construct Binary Tree from Preorder and Inorder Traversal 30.6% Medium

106 Construct Binary Tree from Inorder and Postorder Traversal 30.8% Medium

108 Convert Sorted Array to Binary Search Tree 40.2% Medium

109 Convert Sorted List to Binary Search Tree 32.6% Medium

113 Path Sum II 31.2% Medium

114 Flatten Binary Tree to Linked List 33.4% Medium

116 Populating Next Right Pointers in Each Node 36.8% Medium

120 Triangle 32.3% Medium

122 Best Time to Buy and Sell Stock II 45.3% Medium

127 Word Ladder 19.3% Medium

129 Sum Root to Leaf Numbers 35.0% Medium

130 Surrounded Regions 17.4% Medium

131 Palindrome Partitioning 30.6% Medium

133 Clone Graph 25.0% Medium

134 Gas Station 28.5% Medium

137 Single Number II 40.0% Medium

139 Word Break 28.0% Medium

142 Linked List Cycle II 31.2% Medium

143 Reorder List 24.5% Medium

144 Binary Tree Preorder Traversal 42.8% Medium

147 Insertion Sort List 31.6% Medium

148 Sort List 27.2% Medium

150 Evaluate Reverse Polish Notation 25.7% Medium

151 Reverse Words in a String 15.7% Medium

152 Maximum Product Subarray 24.3% Medium

153 Find Minimum in Rotated Sorted Array 38.4% Medium

156 Binary Tree Upside Down 42.4% Medium

161 One Edit Distance 30.3% Medium

162 Find Peak Element 35.6% Medium

163 Missing Ranges 28.5% Medium

166 Fraction to Recurring Decimal 16.7% Medium

167 Two Sum II - Input array is sorted 47.9% Medium

173 Binary Search Tree Iterator 38.7% Medium

179 Largest Number 21.3% Medium

186 Reverse Words in a String II 28.5% Medium

187 Repeated DNA Sequences 29.3% Medium

199 Binary Tree Right Side View 38.4% Medium

200 Number of Islands 32.0% Medium

201 Bitwise AND of Numbers Range 33.0% Medium

207 Course Schedule 30.2% Medium

208 Implement Trie (Prefix Tree) 25.6% Medium

209 Minimum Size Subarray Sum 28.4% Medium

210 Course Schedule II 25.3% Medium

211 Add and Search Word - Data structure design 20.1% Medium

213 House Robber II 32.9% Medium

215 Kth Largest Element in an Array 37.2% Medium

216 Combination Sum III 41.5% Medium

220 Contains Duplicate III 19.5% Medium

221 Maximal Square 26.8% Medium

222 Count Complete Tree Nodes 26.9% Medium

227 Basic Calculator II 27.9% Medium

228 Summary Ranges 27.8% Medium

229 Majority Element II 27.6% Medium

230 Kth Smallest Element in a BST 41.7% Medium

236 Lowest Common Ancestor of a Binary Tree 29.2% Medium

238 Product of Array Except Self 46.8% Medium

240 Search a 2D Matrix II 37.8% Medium

241 Different Ways to Add Parentheses 40.9% Medium

244 Shortest Word Distance II 34.8% Medium

245 Shortest Word Distance III 49.2% Medium

247 Strobogrammatic Number II 38.0% Medium

250 Count Univalue Subtrees 40.0% Medium

251 Flatten 2D Vector 38.5% Medium

253 Meeting Rooms II 38.0% Medium

254 Factor Combinations 40.0% Medium

255 Verify Preorder Sequence in Binary Search Tree 38.7% Medium

256 Paint House 45.7% Medium

259 3Sum Smaller 40.5% Medium

260 Single Number III 48.9% Medium

261 Graph Valid Tree 36.3% Medium

264 Ugly Number II 31.3% Medium

267 Palindrome Permutation II 30.8% Medium

268 Missing Number 43.3% Medium

271 Encode and Decode Strings 26.5% Medium

274 H-Index 31.9% Medium

275 H-Index II 33.5% Medium

277 Find the Celebrity 35.4% Medium

279 Perfect Squares 34.7% Medium

280 Wiggle Sort 54.5% Medium

281 Zigzag Iterator 47.9% Medium

284 Peeking Iterator 35.0% Medium

285 Inorder Successor in BST 36.2% Medium

286 Walls and Gates 42.0% Medium

289 Game of Life 36.2% Medium

294 Flip Game II 44.9% Medium

298 Binary Tree Longest Consecutive Sequence 39.7% Medium

300 Longest Increasing Subsequence 37.2% Medium

304 Range Sum Query 2D - Immutable 22.7% Medium

306 Additive Number 27.1% Medium

307 Range Sum Query - Mutable 18.5% Medium

309 Best Time to Buy and Sell Stock with Cooldown 39.4% Medium

310 Minimum Height Trees 28.3% Medium

311 Sparse Matrix Multiplication 50.4% Medium

313 Super Ugly Number 36.8% Medium

314 Binary Tree Vertical Order Traversal 34.9% Medium

318 Maximum Product of Word Lengths 41.9% Medium

319 Bulb Switcher 41.8% Medium

320 Generalized Abbreviation 43.0% Medium

322 Coin Change 25.9% Medium

323 Number of Connected Components in an Undirected Graph 46.2% Medium

324 Wiggle Sort II 25.0% Medium

325 Maximum Size Subarray Sum Equals k 41.4% Medium

328 Odd Even Linked List 41.6% Medium

331 Verify Preorder Serialization of a Binary Tree 34.9% Medium

332 Reconstruct Itinerary 27.7% Medium

333 Largest BST Subtree 29.6% Medium

334 Increasing Triplet Subsequence 37.6% Medium

337 House Robber III 41.3% Medium

338 Counting Bits 59.3% Medium

341 Flatten Nested List Iterator 38.2% Medium

343 Integer Break 44.6% Medium

347 Top K Frequent Elements 45.6% Medium

348 Design Tic-Tac-Toe 45.0% Medium

351 Android Unlock Patterns 42.3% Medium

353 Design Snake Game 25.1% Medium

355 Design Twitter 24.1% Medium

356 Line Reflection 29.7% Medium

357 Count Numbers with Unique Digits 44.8% Medium

360 Sort Transformed Array 43.0% Medium

361 Bomb Enemy 37.8% Medium

362 Design Hit Counter 52.4% Medium

364 Nested List Weight Sum II 50.2% Medium

365 Water and Jug Problem 25.9% Medium

366 Find Leaves of Binary Tree 56.7% Medium

367 Valid Perfect Square 37.2% Medium

368 Largest Divisible Subset 32.7% Medium

369 Plus One Linked List 52.2% Medium

370 Range Addition 52.8% Medium

372 Super Pow 32.9% Medium

373 Find K Pairs with Smallest Sums 29.5% Medium

375 Guess Number Higher or Lower II 34.7% Medium

376 Wiggle Subsequence 34.3% Medium

377 Combination Sum IV 41.5% Medium

378 Kth Smallest Element in a Sorted Matrix 42.9% Medium

379 Design Phone Directory 29.1% Medium

382 Linked List Random Node 46.1% Medium

384 Shuffle an Array 44.7% Medium

385 Mini Parser 29.4% Medium

386 Lexicographical Numbers 39.0% Medium

388 Longest Absolute File Path 34.4% Medium

390 Elimination Game 37.4% Medium

392 Is Subsequence 44.1% Medium

393 UTF-8 Validation 35.2% Medium

394 Decode String 39.7% Medium

395 Longest Substring with At Least K Repeating Characters 35.3% Medium

397 Integer Replacement 28.9% Medium

398 Random Pick Index 39.0% Medium

399 Evaluate Division 39.2% Medium

402 Remove K Digits 25.9% Medium

406 Queue Reconstruction by Height 54.1% Medium

413 Arithmetic Slices 54.2% Medium

416 Partition Equal Subset Sum 37.2% Medium

417 Pacific Atlantic Water Flow 32.7% Medium

418 Sentence Screen Fitting 26.9% Medium

419 Battleships in a Board 59.8% Medium

421 Maximum XOR of Two Numbers in an Array 40.6% Medium

423 Reconstruct Original Digits from English 41.7% Medium

424 Longest Repeating Character Replacement 39.6% Medium

435 Non-overlapping Intervals 39.8% Medium

436 Find Right Interval 41.9% Medium

439 Ternary Expression Parser 49.3% Medium

442 Find All Duplicates in an Array 45.6% Medium

444 Sequence Reconstruction 20.1% Medium

445 Add Two Numbers II 44.5% Medium

449 Serialize and Deserialize BST 40.1% Medium

450 Delete Node in a BST 32.4% Medium

451 Sort Characters By Frequency 50.3% Medium

452 Minimum Number of Arrows to Burst Balloons 41.9% Medium

454 4Sum II 41.2% Medium

456 132 Pattern 27.3% Medium

462 Minimum Moves to Equal Array Elements II 50.3% Medium

464 Can I Win 21.4% Medium

467 Unique Substrings in Wraparound String 28.4% Medium

468 Validate IP Address 21.8% Medium

469 Convex Polygon 24.6% Medium

473 Matchsticks to Square 27.5% Medium

474 Ones and Zeroes 32.7% Medium

477 Total Hamming Distance 40.1% Medium

Hard 集合

4 Median of Two Sorted Arrays 20.7% Hard

10 Regular Expression Matching 23.4% Hard

23 Merge k Sorted Lists 25.7% Hard

25 Reverse Nodes in k-Group 29.6% Hard

30 Substring with Concatenation of All Words 21.6% Hard

32 Longest Valid Parentheses 23.0% Hard

33 Search in Rotated Sorted Array 31.8% Hard

37 Sudoku Solver 27.8% Hard

41 First Missing Positive 24.9% Hard

42 Trapping Rain Water 35.1% Hard

44 Wildcard Matching 18.9% Hard

45 Jump Game II 26.0% Hard

51 N-Queens 28.8% Hard

52 N-Queens II 42.5% Hard

56 Merge Intervals 28.2% Hard

57 Insert Interval 26.2% Hard

65 Valid Number 12.6% Hard

68 Text Justification 17.9% Hard

72 Edit Distance 30.5% Hard

76 Minimum Window Substring 23.6% Hard

84 Largest Rectangle in Histogram 25.5% Hard

85 Maximal Rectangle 25.8% Hard

87 Scramble String 28.1% Hard

97 Interleaving String 23.9% Hard

99 Recover Binary Search Tree 28.6% Hard

115 Distinct Subsequences 30.5% Hard

117 Populating Next Right Pointers in Each Node II 33.4% Hard

123 Best Time to Buy and Sell Stock III 28.1% Hard

124 Binary Tree Maximum Path Sum 24.9% Hard

126 Word Ladder II 13.6% Hard

128 Longest Consecutive Sequence 35.1% Hard

132 Palindrome Partitioning II 23.3% Hard

135 Candy 23.8% Hard

138 Copy List with Random Pointer 26.5% Hard

140 Word Break II 22.0% Hard

145 Binary Tree Postorder Traversal 38.2% Hard

146 LRU Cache 16.0% Hard

149 Max Points on a Line 15.5% Hard

154 Find Minimum in Rotated Sorted Array II 36.0% Hard

158 Read N Characters Given Read4 II - Call multiple times 24.2% Hard

159 Longest Substring with At Most Two Distinct Characters 39.3% Hard

164 Maximum Gap 28.5% Hard

174 Dungeon Game 22.8% Hard

188 Best Time to Buy and Sell Stock IV 23.7% Hard

212 Word Search II 22.1% Hard

214 Shortest Palindrome 22.7% Hard

218 The Skyline Problem 25.3% Hard

224 Basic Calculator 25.3% Hard

233 Number of Digit One 27.2% Hard

239 Sliding Window Maximum 31.2% Hard

248 Strobogrammatic Number III 30.2% Hard

265 Paint House II 37.0% Hard

269 Alien Dictionary 22.5% Hard

272 Closest Binary Search Tree Value II 37.2% Hard

273 Integer to English Words 20.9% Hard

282 Expression Add Operators 28.3% Hard

287 Find the Duplicate Number 41.6% Hard

291 Word Pattern II 37.4% Hard

295 Find Median from Data Stream 23.2% Hard

296 Best Meeting Point 50.0% Hard

297 Serialize and Deserialize Binary Tree 31.3% Hard

301 Remove Invalid Parentheses 34.5% Hard

302 Smallest Rectangle Enclosing Black Pixels 43.3% Hard

305 Number of Islands II 37.9% Hard

308 Range Sum Query 2D - Mutable 19.9% Hard

312 Burst Balloons 41.4% Hard

315 Count of Smaller Numbers After Self 33.4% Hard

316 Remove Duplicate Letters 28.3% Hard

317 Shortest Distance from All Buildings 33.2% Hard

321 Create Maximum Number 23.8% Hard

327 Count of Range Sum 28.6% Hard

329 Longest Increasing Path in a Matrix 35.1% Hard

330 Patching Array 31.3% Hard

335 Self Crossing 23.7% Hard

336 Palindrome Pairs 24.3% Hard

340 Longest Substring with At Most K Distinct Characters 38.6% Hard

352 Data Stream as Disjoint Intervals 38.7% Hard

354 Russian Doll Envelopes 31.4% Hard

358 Rearrange String k Distance Apart 31.7% Hard

363 Max Sum of Rectangle No Larger Than K 32.0% Hard

380 Insert Delete GetRandom O(1) 38.0% Hard

381 Insert Delete GetRandom O(1) - Duplicates allowed 27.7% Hard

391 Perfect Rectangle 23.3% Hard

403 Frog Jump 30.2% Hard

407 Trapping Rain Water II 35.0% Hard

410 Split Array Largest Sum 31.1% Hard

411 Minimum Unique Word Abbreviation 30.9% Hard

420 Strong Password Checker 21.5% Hard

425 Word Squares 42.0% Hard

432 All O`one Data Structure 28.4% Hard

440 K-th Smallest in Lexicographical Order 21.4% Hard

446 Arithmetic Slices II - Subsequence 22.0% Hard

465 Optimal Account Balancing 27.5% Hard

466 Count The Repetitions 24.6% Hard

471 Encode String with Shortest Length 45.9% Hard

472 Concatenated Words 28.2% Hard