Skip to content

Commit deea10f

Browse files
committed
重新整理LeetCode面试题系列代码.
1 parent f7666ea commit deea10f

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
# leetcode-python
2-
Python玩转LeetCode系列文章代码
1+
# Python玩转LeetCode
2+
Python 玩转 LeetCode 的学习之路
3+
4+
- [Python 玩转 LeetCode 面试题](http://www.justdopython.com/category/#/Leetcode面试题)
5+
- [Leetcode 面试系列 第1天:Leetcode 89 - 格雷码](http://www.justdopython.com/2019/09/09/python-leetcode89-gary-code/)
6+
- [LeetCode 面试系列 第2天:No.136 - 只出现一次的数](http://www.justdopython.com/2019/09/13/python-leetcode136-single-number/)
7+
- [LeetCode 面试系列 第3天:No.67 - 二进制数求和](http://www.justdopython.com/2019/09/14/python-leetcode67-add-binary/)
8+
- [LeetCode 面试系列 第4天:No.202 - 快乐数](http://www.justdopython.com/2019/09/28/python-leetcode202-happy-number/)
9+
- [LeetCode 面试系列 第5天:No.204 - 统计质数](http://www.justdopython.com/2019/10/04/python-leetcode204-count-primes/)
10+
11+
关注公众号:python技术,回复"python"一起学习交流
12+
13+
![](http://favorites.ren/assets/images/python.jpg)

leetcode-067/leetcode67.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def addBinary(self, a: str, b: str) -> str:
3+
num1 = int(a, 2)
4+
num2 = int(b, 2)
5+
sum0 = num1 + num2
6+
return format(sum0, 'b')
7+
8+
# 测试
9+
sol = Solution()
10+
print(sol.addBinary('11', '1101'))

leetcode-089/leetcode89.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
class Solution:
4+
def grayCode(self, n: int) -> List[int]:
5+
res = []
6+
for i in range(1 << n):
7+
res.append((i >> 1) ^ i)
8+
return res
9+
10+
# Below is testing
11+
obj = Solution()
12+
print(obj.grayCode(2))

leetcode-136/leetcode136-method1.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
class Solution:
4+
def singleNumber(self, nums: List[int]) -> int:
5+
res = 0
6+
d = dict()
7+
for num in nums:
8+
if num not in d:
9+
d[num] = 1
10+
else:
11+
d[num] += 1
12+
res = next(k for k, val in d.items() if val == 1)
13+
return res
14+
15+
#以下是测试
16+
sol = Solution()
17+
print(sol.singleNumber([6, 4, 6, 2, 4]))

leetcode-136/solution-method2.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
class Solution:
4+
def singleNumber(self, nums: List[int]) -> int:
5+
r = 0
6+
for i in nums:
7+
r ^= i
8+
return r
9+
10+
#以下是测试
11+
sol = Solution()
12+
print(sol.singleNumber([6, 4, 6, 2, 4]))

leetcode-202/leetcode202.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isHappy(self, n: int) -> bool:
3+
unhappy = set()
4+
while n not in unhappy and n != 1:
5+
unhappy.add(n)
6+
n = self.GetSquareSum(n)
7+
return n == 1
8+
9+
def GetSquareSum(self, n: int) -> bool:
10+
sum0 = 0
11+
while n > 0:
12+
r = n - int(n/10)*10
13+
n = int(n/10)
14+
sum0 += r * r
15+
return sum0
16+
17+
sol = Solution()
18+
print(sol.isHappy(19))

leetcode-204/leetcode204-method1.py

Whitespace-only changes.

0 commit comments

Comments
 (0)