8000 update at 2018-05-28 · robertchen/leetcode@23a3f14 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23a3f14

Browse files
committed
update at 2018-05-28
1 parent 1060ebf commit 23a3f14

File tree

11 files changed

+146
-72
lines changed

11 files changed

+146
-72
lines changed

071-simplify-path/simplify-path.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
# path = "/home/", => "/home"
88
# path = "/a/./b/../../c/", => "/c"
99
#
10-
# click to show corner cases.
11-
#
1210
# Corner Cases:
1311
#
14-
#  
15-
#
16-
#  
17-
#
1812
#
1913
# Did you consider the case where path = "/../"?
2014
# In this case, you should return "/".

227-basic-calculator-ii/basic-calculator-ii.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,33 @@
55
#
66
# The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
77
#
8-
# You may assume that the given expression is always valid.
8+
# Example 1:
99
#
10-
# Some examples:
1110
#
12-
# "3+2*2" = 7
13-
# " 3/2 " = 1
14-
# " 3+5 / 2 " = 5
11+
# Input: "3+2*2"
12+
# Output: 7
1513
#
1614
#
15+
# Example 2:
1716
#
1817
#
19-
# Note: Do not use the eval built-in library function.
18+
# Input: " 3/2 "
19+
# Output: 1
20+
#
21+
# Example 3:
22+
#
23+
#
24+
# Input: " 3+5 / 2 "
25+
# Output: 5
26+
#
27+
#
28+
# Note:
29+
#
30+
#
31+
# You may assume that the given expression is always valid.
32+
# Do not use the eval built-in library function.
2033
#
2134
#
22-
# Credits:Special thanks to @ts for adding this problem and creating all test cases.
2335

2436

2537
class Solution(object):

240-search-a-2d-matrix-ii/search-a-2d-matrix-ii.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
# Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
55
#
66
#
7+
# Integers in each row are sorted in ascending from left to right.
8+
# Integers in each column are sorted in ascending from top to bottom.
79
#
8-
# Integers in each row are sorted in ascending from left to right.
9-
# Integers in each column are sorted in ascending from top to bottom.
10-
#
11-
#
12-
#
13-
#
14-
# For example,
1510
#
1611
# Consider the following matrix:
1712
#
@@ -25,8 +20,20 @@
2520
# ]
2621
#
2722
#
28-
# Given target = 5, return true.
29-
# Given target = 20, return false.
23+
# Example 1:
24+
#
25+
#
26+
# Input: matrix, target = 5
27+
# Output: true
28+
#
29+
#
30+
# Example 2:
31+
#
32+
#
33+
# Input: matrix, target = 20
34+
# Output: false
35+
#
36+
#
3037

3138

3239
class Solution(object):

242-valid-anagram/valid-anagram.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# -*- coding:utf-8 -*-
22

33

4-
# Given two strings s and t, write a function to determine if t is an anagram of s.
4+
# Given two strings s and t , write a function to determine if t is an anagram of s.
5+
#
6+
# Example 1:
7+
#
8+
#
9+
# Input: s = "anagram", t = "nagaram"
10+
# Output: true
11+
#
12+
#
13+
# Example 2:
14+
#
15+
#
16+
# Input: s = "rat", t = "car"
17+
# Output: false
518
#
6-
# For example,
7-
# s = "anagram", t = "nagaram", return true.
8-
# s = "rat", t = "car", return false.
919
#
1020
# Note:
1121
# You may assume the string contains only lowercase alphabets.

263-ugly-number/ugly-number.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33

44
# Write a program to check whether a given number is an ugly number.
55
#
6-
# Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
6+
# Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
77
#
8-
# Note:
8+
# Example 1:
99
#
1010
#
11-
# 1 is typically treated as an ugly number.
12-
# Input is within the 32-bit signed integer range.
11+
# Input: 6
12+
# Output: true
13+
# Explanation: 6 = 2 × 3
14+
#
15+
# Example 2:
16+
#
17+
#
18+
# Input: 8
19+
# Output: true
20+
# Explanation: 8 = 2 × 2 × 2
21+
#
22+
#
23+
# Example 3:
1324
#
1425
#
26+
# Input: 14
27+
# Output: false
28+
# Explanation: 14 is not ugly since it includes another prime factor 7.
29+
#
30+
#
31+
# Note:
32+
#
33+
#
34+
# 1 is typically treated as an ugly number.
35+
# Input is within the 32-bit signed integer range: [−231,  231 − 1].
1536
#
16-
# Credits:
17-
# Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
1837
#
1938

2039

264-ugly-number-ii/ugly-number-ii.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# -*- coding:utf-8 -*-
22

33

4-
#
54
# Write a program to find the n-th ugly number.
65
#
6+
# Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. 
7+
#
8+
# Example:
79
#
810
#
9-
# Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
11+
# Input: n = 10
12+
# Output: 12
13+
# Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
1014
#
15+
# Note:  
1116
#
1217
#
13-
# Note that 1 is typically treated as an ugly number, and n does not exceed 1690.
18+
# 1 is typically treated as an ugly number.
19+
# n does not exceed 1690.
1420
#
1521
#
16-
# Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
1722

1823

1924
class Solution(object):

274-h-index/h-index.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# -*- coding:utf-8 -*-
22

33

4-
#
54
# Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
65
#
7-
#
8-
#
96
# According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
107
#
8+
# Example:
119
#
1210
#
13-
# For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
14-
#
15-
#
16-
#
17-
# Note: If there are several possible values for h, the maximum one is taken as the h-index.
11+
# Input: citations = [3,0,6,1,5]
12+
# Output: 3
13+
# Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
14+
# received 3, 0, 6, 1, 5 citations respectively.
15+
#   Since the researcher has 3 papers with at least 3 citations each and the remaining
16+
#   two with no more than 3 citations each, his h-index is 3.
1817
#
18+
# Note: If there are several possible values for h, the maximum one is taken as the h-index.
1919
#
20-
# Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
2120

2221

2322
class Solution(object):

275-h-index-ii/h-index-ii.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
# -*- coding:utf-8 -*-
22

33

4+
# Given an array of citations in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
45
#
5-
# Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
6+
# According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than hcitations each."
7+
#
8+
# Example:
9+
#
10+
#
11+
# Input: citations = [0,1,3,5,6]
12+
# Output: 3
13+
# Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
14+
# received 0, 1, 3, 5, 6 citations respectively.
15+
#   Since the researcher has 3 papers with at least 3 citations each and the remaining
16+
#   two with no more than 3 citations each, his h-index is 3.
17+
#
18+
# Note: If there are several possible values for h, the maximum one is taken as the h-index.
619
#
720

821

313-super-ugly-number/super-ugly-number.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# -*- coding:utf-8 -*-
22

33

4+
# Write a program to find the nth super ugly number.
45
#
5-
# Write a program to find the nth super ugly number.
6+
# Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k.
67
#
8+
# Example:
79
#
810
#
9-
# Super ugly numbers are positive numbers whose all prime factors are in the given prime list
10-
# primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
11-
# is the sequence of the first 12 super ugly numbers given primes
12-
# = [2, 7, 13, 19] of size 4.
11+
# Input: n = 12, primes = [2,7,13,19]
12+
# Output: 32
13+
# Explanation: [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12
14+
# super ugly numbers given primes = [2,7,13,19] of size 4.
1315
#
16+
# Note:
1417
#
1518
#
16-
# Note:
17-
# (1) 1 is a super ugly number for any given primes.
18-
# (2) The given numbers in primes are in ascending order.
19-
# (3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
20-
# (4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
19+
# 1 is a super ugly number for any given primes.
20+
# The given numbers in primes are in ascending order.
21+
# 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
22+
# The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
2123
#
2224
#
23-
# Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
2425

2526

2627
class Solution(object):

324-wiggle-sort-ii/wiggle-sort-ii.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# -*- coding:utf-8 -*-
22

33

4+
# Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
45
#
5-
# Given an unsorted array nums, reorder it such that
6-
# nums[0] < nums[1] > nums[2] < nums[3]....
6+
# Example 1:
77
#
88
#
9 D03B +
# Input: nums = [1, 5, 1, 1, 6, 4]
10+
# Output: One possible answer is [1, 4, 1, 5, 1, 6].
911
#
10-
# Example:
11-
# (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
12-
# (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
12+
# Example 2:
1313
#
1414
#
15+
# Input: nums = [1, 3, 2, 2, 3, 1]
16+
# Output: One possible answer is [2, 3, 1, 3, 1, 2].
1517
#
16-
# Note:
17-
# You may assume all input has valid answer.
18+
# Note:
19+
# You may assume all input has valid answer.
1820
#
21+
# Follow Up:
22+
# Can you do it in O(n) time and/or in-place with O(1) extra space?
1923
#
20-
#
21-
# Follow Up:
22-
# Can you do it in O(n) time and/or in-place with O(1) extra space?
23-
#
24-
#
25-
# Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
2624

2725

2826
class Solution(object):

0 commit comments

Comments
 (0)
0