File tree Expand file tree Collapse file tree 4 files changed +142
-0
lines changed Expand file tree Collapse file tree 4 files changed +142
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ You are climbing a stair case. It takes n steps to reach to the top.
3
+
4
+ Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5
+
6
+ Note: Given n will be a positive integer.
7
+
8
+ Example 1:
9
+
10
+ Input: 2
11
+ Output: 2
12
+ Explanation: There are two ways to climb to the top.
13
+ 1. 1 step + 1 step
14
+ 2. 2 steps
15
+ '''
16
+
17
+ class Solution (object ):
18
+ def climbStairs (self , n ):
19
+ """
20
+ :type n: int
21
+ :rtype: int
22
+ """
23
+ if n == 0 :
24
+ return 0
25
+
26
+ dp = [0 ]* n
27
+ dp [0 ], dp [1 ] = 1 , 2
28
+
29
+ for index in range (2 , n ):
30
+ dp [index ] = dp [index - 1 ] + dp [index - 2 ]
31
+ return dp [n - 1 ]
32
+
33
+ # Time: O(N)
34
+ # Space: O(N)
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given an absolute path for a file (Unix-style), simplify it.
3
+
4
+ For example,
5
+ path = "/home/", => "/home"
6
+ path = "/a/./b/../../c/", => "/c"
7
+ '''
8
+
9
+ class Solution (object ):
10
+ def simplifyPath (self , path ):
11
+ """
12
+ :type path: str
13
+ :rtype: str
14
+ """
15
+
16
+ result = "/"
17
+ stack = []
18
+
19
+ index = 0
20
+ while index < len (path ):
21
+ if path [index ] == '/' :
22
+ index += 1
23
+ continue
24
+
25
+
26
+ curr_str = ""
27
+ while index < len (path ) and path [index ] != '/' :
28
+ curr_str += path [index ]
29
+ index += 1
30
+
31
+ if curr_str == '.' or curr_str == "" :
32
+ index += 1
33
+ continue
34
+ elif curr_str == ".." :
35
+ if stack :
36
+ stack .pop ()
37
+ index += 1
38
+ else :
39
+ stack .append (curr_str )
40
+ index += 1
41
+
42
+ for index in range (len (stack )):
43
+ if index != len (stack ) - 1 :
44
+ result += stack [index ] + '/'
45
+ else :
46
+ result += stack [index ]
47
+
48
+ return result
49
+
50
+ # Time: O(N)
51
+ # Space: O(N)
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def setZeroes (self , matrix ):
3
+ """
4
+ :type matrix: List[List[int]]
5
+ :rtype: void Do not return anything, modify matrix in-place instead.
6
+ """
7
+ col0 = 1
8
+ for row in range (len (matrix )):
9
+ if matrix [row ][0 ] == 0 :
10
+ col0 = 0
11
+ for col in range (1 , len (matrix [0 ])):
12
+ if matrix [row ][col ] == 0 :
13
+ matrix [row ][0 ] = 0
14
+ matrix [0 ][col ] = 0
15
+ for row in range (len (matrix )- 1 , - 1 , - 1 ):
16
+ for col in range (len (matrix [0 ])- 1 , 0 , - 1 ):
17
+ if matrix [row ][0 ] == 0 or matrix [0 ][col ] == 0 :
18
+ matrix [row ][col ] = 0
19
+ if col0 == 0 :
20
+ matrix [row ][0 ] = 0
21
+
Original file line number Diff line number Diff line change
1
+ '''
2
+ Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
3
+
4
+ Integers in each row are sorted from left to right.
5
+ The first integer of each row is greater than the last integer of the previous row.
6
+ Example 1:
7
+
8
+ Input:
9
+ matrix = [
10
+ [1, 3, 5, 7],
11
+ [10, 11, 16, 20],
12
+ [23, 30, 34, 50]
13
+ ]
14
+ target = 3
15
+ Output: true
16
+ '''
17
+ class Solution (object ):
18
+ def searchMatrix (self , matrix , target ):
19
+ """
20
+ :type matrix: List[List[int]]
21
+ :type target: int
22
+ :rtype: bool
23
+ """
24
+
25
+ if not matrix :
26
+ return 0
27
+ left , right = 0 , len (matrix [0 ])- 1
28
+
29
+ while left < len (matrix ) and right >= 0 :
30
+ if matrix [left ][right ] == target :
31
+ return True
32
+ elif matrix [left ][right ] < target :
33
+ left += 1
34
+ else :
35
+ right -= 1
36
+ return False
You can’t perform that action at this time.
0 commit comments