Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6c4471

Browse files
Update
Update
1 parent 6bd7dc3 commit b6c4471

6 files changed

+205
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a sorted array of integers, find the starting and ending position of a given target value.
5+
6+
Your algorithm's runtime complexity must be in the order of O(log n).
7+
8+
If the target is not found in the array, return [-1, -1].
9+
10+
For example,
11+
Given [5, 7, 7, 8, 8, 10] and target value 8,
12+
return [3, 4].
13+
'''
14+
15+
16+
class Solution(object):
17+
def searchRange(self, nums, target):
18+
"""
19+
:type nums: List[int]
20+
:type target: int
21+
:rtype: List[int]
22+
"""
23+
for i in range(len(nums)):
24+
if nums[i] == target:
25+
left = i
26+
break
27+
else:
28+
return [-1, 1]
29+
# find the index of the rightmost appearance of `target` (by reverse
30+
# iteration). it is guaranteed to appear.
31+
for j in range(len(nums) - 1, -1, -1):
32+
if nums[j] == target:
33+
right = j
34+
break
35+
return [left, right]
36+
37+
38+
39+
40+
41+
if __name__ == "__main__":
42+
assert Solution().searchRange([5, 7, 7, 8, 8, 10], 8) == [3, 4]
43+
assert Solution().searchRange([5, 7, 7, 8, 8, 10], 5) == [0, 0]
44+
assert Solution().searchRange([5, 7, 7, 8, 8, 10], 7) == [1, 2]
45+
assert Solution().searchRange([5, 7, 7, 8, 8, 10], 10) == [5, 5]

‎Python3/038_Count_and_Say.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
The count-and-say sequence is the sequence of integers beginning as follows:
5+
1, 11, 21, 1211, 111221, ...
6+
7+
1 is read off as "one 1" or 11.
8+
11 is read off as "two 1s" or 21.
9+
21 is read off as "one 2, then one 1" or 1211.
10+
Given an integer n, generate the nth sequence.
11+
12+
Note: The sequence of integers will be represented as a string.
13+
'''
14+
15+
16+
class Solution(object):
17+
def countAndSay(self, n):
18+
"""
19+
:type n: int
20+
:rtype: str
21+
"""
22+
result = "1"
23+
for __ in range(1, n):
24+
result = self.getNext(result)
25+
return result
26+
27+
def getNext(self, s):
28+
result = []
29+
start = 0
30+
while start < len(s):
31+
curr = start + 1
32+
while curr < len(s) and s[start] == s[curr]:
33+
curr += 1
34+
result.extend((str(curr - start), s[start]))
35+
start = curr
36+
return "".join(result)
37+
38+
39+
if __name__ == "__main__":
40+
assert Solution().countAndSay(5) == "111221"
41+
42+
43+

‎Python3/050_Pow(x, n).py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Implement pow(x, n).
5+
'''
6+
7+
8+
class Solution(object):
9+
def myPow(self, x, n):
10+
"""
11+
:type x: float
12+
:type n: int
13+
:rtype: float
14+
"""
15+
flag = 1 if n >= 0 else -1
16+
result = 1
17+
n = abs(n)
18+
while n > 0:
19+
if n & 1 == 1:
20+
result *= x
21+
n >>= 1
22+
x *= x
23+
if flag < 0:
24+
result = 1 / result
25+
return result
26+
27+
28+
if __name__ == "__main__":
29+
assert Solution().myPow(2, 5) == 32
30+
assert Solution().myPow(2.1, 2) == 4.41

‎Python3/062_Unique_Paths_method1.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
5+
6+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7+
8+
How many possible unique paths are there?
9+
10+
11+
Above is a 3 x 7 grid. How many possible unique paths are there?
12+
13+
Note: m and n will be at most 100.
14+
'''
15+
16+
17+
class Solution(object):
18+
def uniquePaths(self, m, n):
19+
"""
20+
:type m: int
21+
:type n: int
22+
:rtype: int
23+
"""
24+
dp = [[1 for __ in range(n)] for __ in range(m)]
25+
for i in range(1, n):
26+
for j in range(1, m):
27+
dp[j][i] = dp[j - 1][i] + dp[j][i - 1]
28+
return dp[m - 1][n - 1]
29+
30+
31+
if __name__ == "__main__":
32+
assert Solution().uniquePaths(3, 7) == 28

‎Python3/062_Unique_Paths_method2.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
5+
6+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7+
8+
How many possible unique paths are there?
9+
10+
11+
Above is a 3 x 7 grid. How many possible unique paths are there?
12+
13+
Note: m and n will be at most 100.
14+
'''
15+
import math
16+
17+
18+
class Solution(object):
19+
def uniquePaths(self, m, n):
20+
"""
21+
:type m: int
22+
:type n: int
23+
:rtype: int
24+
"""
25+
m -= 1
26+
n -= 1
27+
return int(math.factorial(m + n) / (math.factorial(m) * math.factorial(n)))
28+
29+
30+
if __name__ == "__main__":
31+
assert Solution().uniquePaths(3, 7) == 28

‎Python3/069_Sqrt(x).py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Implement int sqrt(int x).
5+
6+
Compute and return the square root of x.
7+
'''
8+
9+
10+
class Solution(object):
11+
def mySqrt(self, x):
12+
"""
13+
:type x: int
14+
:rtype: int
15+
"""
16+
result = 1.0
17+
while abs(result ** 2 - x) > 0.1:
18+
result = (result + x / result) / 2
19+
return int(result)
20+
21+
22+
if __name__ == "__main__":
23+
assert Solution().mySqrt(5) == 2
24+
assert Solution().mySqrt(0) == 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /