Skip to content

Commit 7ce6c8a

Browse files
authored
Add files via upload
1 parent 56c2663 commit 7ce6c8a

File tree

68 files changed

+804
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+804
-0
lines changed

leetcode/009.回文数.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution(object):
2+
def isPalindrome(self, x):
3+
return str(x) == str(x)[::-1]

leetcode/069.x 的平方根.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 方法一:数学函数
2+
class Solution(object):
3+
def mySqrt(self, x):
4+
return int(math.sqrt(x))
5+
6+
# 方法二:1/2次方取整
7+
class Solution(object):
8+
def mySqrt(self, x):
9+
return int(x**(1/2.0))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution(object):
2+
def singleNumber(self, nums):
3+
d = collections.Counter(nums)
4+
res = [k for k in d if d[k]==1]
5+
return res[0]

leetcode/155.最小栈.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class MinStack(object):
2+
3+
def __init__(self):
4+
self.stack = []
5+
6+
def push(self, x):
7+
self.stack.append(x)
8+
9+
def pop(self):
10+
self.stack.pop()
11+
12+
def top(self):
13+
return self.stack[-1]
14+
15+
def getMin(self):
16+
return min(self.stack)

leetcode/171.Excel表列序号.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 26进制转10进制。正序
2+
class Solution(object):
3+
def titleToNumber(self, s):
4+
res = 0
5+
for i in range(len(s)):
6+
res += (26**(len(s)-i-1)) * (ord(s[i])-ord('A')+1)
7+
return int(res)
8+
9+
# 逆序
10+
class Solution(object):
11+
def titleToNumber(self, s):
12+
S = list(s)
13+
S.reverse()
14+
res = 0
15+
for i, v in enumerate(S):
16+
res += (26**i) * (ord(v)-ord('A')+1)
17+
return res

leetcode/172.阶乘后的零.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 找规律,将阶乘中的数分解成因子,有1个5则可以和前面分解出来的2搭配 2*5=10,则末尾就会有1个0
2+
class Solution(object):
3+
def trailingZeroes(self, n):
4+
res = 0
5+
while n > 0:
6+
n /= 5
7+
res += n
8+
return res

leetcode/190.颠倒二进制位.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 方法一:将无符号整数转为二进制,用0补满32位,反转后将二进制格式转为十进制
2+
class Solution:
3+
def reverseBits(self, n):
4+
return int(bin(n)[2:].zfill(32)[::-1], 2)
5+
6+
# 方法二:位运算,res每次左移加上n的最后一位,然后n右移去掉最后一位
7+
class Solution:
8+
def reverseBits(self, n):
9+
res = 0
10+
for _ in range(32):
11+
res = (res<<1) + n%2
12+
n >>= 1
13+
return res

leetcode/191.位1的个数.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 输入是一个无符号整数,要先转化为二进制形式,再转为字符串形式统计1的个数
2+
class Solution(object):
3+
def hammingWeight(self, n):
4+
return bin(n).count('1')

leetcode/202.快乐数.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 所有不快乐数的数位平方和计算,最后都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中
2+
class Solution(object):
3+
def isHappy(self, n):
4+
while n != 1:
5+
n = sum(map(lambda x: x**2, map(int, str(n))))
6+
if n == 4:
7+
return False
8+
return True

leetcode/204.计数质数.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 厄拉多塞筛法:先将 2~n 的各个数放入表中,然后在2的上面画一个圆圈,然后划去2的其他倍数;
2+
# 第一个既未画圈又没有被划去的数是3,将它画圈,再划去3的其他倍数;
3+
# 现在既未画圈又没有被划去的第一个数 是5,将它画圈,并划去5的其他倍数……
4+
# 依次类推,一直到所有小于或等于 n 的各数都画了圈或划去为止。
5+
# 这时,表中画了圈的以及未划去的那些数正好就是小于 n 的素数。
6+
class Solution(object):
7+
def countPrimes(self, n):
8+
if n < 3:
9+
return 0
10+
primes = [1]*n
11+
primes[0] = primes[1] = 0
12+
for i in range(2, int(n**0.5)+1):
13+
if primes[i]:
14+
primes[i*i:n:i] = [0]*(len(primes[i*i:n:i]))
15+
return sum(primes)

0 commit comments

Comments
 (0)