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)

leetcode/205.同构字符串.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 方法一:若同构则查找字符的索引相同
2+
class Solution(object):
3+
def isIsomorphic(self, s, t):
4+
for i in range(len(s)):
5+
if s.find(s[i]) != t.find(t[i]):
6+
return False
7+
return True
8+
9+
# 方法二:判断两字符串去重 与 压缩再去重的长度是否相同
10+
class Solution(object):
11+
def isIsomorphic(self, s, t):
12+
if len(s) != len(t):
13+
return False
14+
return len(set(zip(s, t))) == len(set(s)) == len(set(t))

leetcode/225.用队列实现栈.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class MyStack(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+
return self.stack.pop()
11+
12+
def top(self):
13+
return self.stack[-1]
14+
15+
def empty(self):
16+
return self.stack == []

leetcode/231.2的幂.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 因子可全由2组成,除尽2判断最终结果是否等于1
2+
class Solution(object):
3+
def isPowerOfTwo(self, n):
4+
if n==0:
5+
return False
6+
while n%2 == 0:
7+
n /= 2
8+
return n == 1

leetcode/232.用栈实现队列.py

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

leetcode/258.各位相加.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 将字符串拆成当个数字相加,判断结果长度是否大于1,循环此过程
2+
class Solution(object):
3+
def addDigits(self, num):
4+
while len(str(num)) > 1:
5+
num = sum(map(int, [i for i in str(num)]))
6+
return num

leetcode/263.丑数.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 丑数只包含质因数 2, 3, 5,将质因数除尽看结果是否为1
2+
class Solution(object):
3+
def isUgly(self, num):
4+
if num <= 0:
5+
return False
6+
while num%2 == 0:
7+
num /= 2
8+
while num%3 == 0:
9+
num /= 3
10+
while num%5 == 0:
11+
num /= 5
12+
return num == 1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 二分法,取中间版本进行判断,若是好版本则将左起始点设为中间版本之后,若是坏版本则将右起始点设为为中间版本
2+
class Solution(object):
3+
def firstBadVersion(self, n):
4+
l, r = 1, n
5+
while l < r:
6+
mid = (l+r)/2
7+
if isBadVersion(mid):
8+
r = mid
9+
else:
10+
l = mid + 1
11+
return l

leetcode/290.单词模式.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 判断字符串、列表去重 与 压缩再去重的长度是否相同
2+
class Solution(object):
3+
def wordPattern(self, pattern, str):
4+
s = str.split()
5+
if len(pattern) != len(s):
6+
return False
7+
return len(set(zip(pattern, s))) == len(set(pattern)) == len(set(s))

leetcode/292.Nim游戏.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 只要给对手留下4的整数倍个石子就能赢,否则就输
2+
class Solution(object):
3+
def canWinNim(self, n):
4+
return n%4 != 0

leetcode/326.3的幂.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 因子可全由3组成,除尽3判断最终结果是否等于1
2+
class Solution(object):
3+
def isPowerOfThree(self, n):
4+
if n==0:
5+
return False
6+
while n%3 == 0:
7+
n /= 3
8+
return n == 1

leetcode/342.4的幂.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 因子可全由4组成,除尽4判断最终结果是否等于1
2+
class Solution(object):
3+
def isPowerOfFour(self, num):
4+
if num==0:
5+
return False
6+
while num%4 == 0:
7+
num /= 4
8+
return num == 1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 方法一:直接平方根
2+
class Solution(object):
3+
def isPerfectSquare(self, num):
4+
return (num**0.5) == int(num**0.5)
5+
6+
# 方法二:二分法,查找是否存在一个数的平方为给定的正整数
7+
class Solution(object):
8+
def isPerfectSquare(self, num):
9+
l, r = 0, num
10+
while l <= r:
11+
mid = (l+r)/2
12+
square = mid**2
13+
if square > num:
14+
r = mid - 1
15+
elif square < num:
16+
l = mid + 1
17+
else:
18+
return True
19+
return False

leetcode/371.两整数之和.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 getSum(self, a, b):
3+
return sum([a, b])

leetcode/374.猜数字大小.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 根据返回结果重新定位左右区间
2+
class Solution(object):
3+
def guessNumber(self, n):
4+
l, r = 1, n
5+
while l <= r:
6+
mid = (l+r)/2
7+
if guess(mid) == -1:
8+
r = mid - 1
9+
elif guess(mid) == 1:
10+
l = mid + 1
11+
else:
12+
return mid

leetcode/389.找不同.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 方法一:遍历s,在t中移除遍历到的元素,最后剩下一个就是添加的字母
2+
class Solution(object):
3+
def findTheDifference(self, s, t):
4+
t = list(t)
5+
for i in s:
6+
t.remove(i)
7+
return t[0]
8+
9+
# 方法二:遍历t,统计t与s中该字符的个数,若不相同则为添加的字母
10+
class Solution(object):
11+
def findTheDifference(self, s, t):
12+
for i in t:
13+
if t.count(i) != s.count(i):
14+
return i

leetcode/409.最长回文串.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 统计字符个数存放在字典中,遍历字典。字符个数为偶数则直接累加。
2+
# 个数为奇数则减1后累加,由于回文串只能有一个字符是奇次数,故若有字符个数是奇数最后要再加1
3+
class Solution(object):
4+
def longestPalindrome(self, s):
5+
d = collections.Counter(s)
6+
length = k = 0
7+
for key in d:
8+
if d[key]%2 == 0:
9+
length += d[key]
10+
else:
11+
k = 1
12+
length += d[key]-1
13+
return length + k

leetcode/412.Fizz Buzz.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def fizzBuzz(self, n):
3+
res = []
4+
for i in range(1, n+1):
5+
if i%3==0 and i%5==0:
6+
res.append('FizzBuzz')
7+
elif i%3==0:
8+
res.append('Fizz')
9+
elif i%5==0:
10+
res.append('Buzz')
11+
else:
12+
res.append(str(i))
13+
return res

leetcode/415.字符串相加.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# eval()执行字符串表达式
2+
class Solution(object):
3+
def addStrings(self, num1, num2):
4+
return str(eval(num1) + eval(num2))

leetcode/441.排列硬币.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 每增加一行,当前总硬币数增加该行的行号,当前总硬币数小于等于n时循环,大于时则上一行的行号即为所求总行数
2+
class Solution(object):
3+
def arrangeCoins(self, n):
4+
i = nums = 0
5+
while nums <= n:
6+
i += 1
7+
nums += i
8+
return i-1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 方法一:每次移动可以使n-1个元素增加1,即使最大元素减1,循环达到所有元素值都为最小元素值
2+
class Solution(object):
3+
def minMoves(self, nums):
4+
min_v = min(nums)
5+
count = 0
6+
for num in nums:
7+
count += num-min_v
8+
return count
9+
10+
# 方法二:即方法一同样思路另一种写法
11+
class Solution(object):
12+
def minMoves(self, nums):
13+
return sum(nums) - min(nums) * len(nums)

leetcode/455.分发饼干.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 用两个指针指向两个排序后数组的首部,比较两个指针指向的元素。若饼干尺寸满足胃口值则两指针各前进一步,否则饼干指针前进一步
2+
class Solution(object):
3+
def findContentChildren(self, g, s):
4+
g.sort()
5+
s.sort()
6+
len_g, len_s = len(g), len(s)
7+
i = j = 0
8+
while i<len_g and j<len_s:
9+
if g[i] <= s[j]:
10+
i += 1
11+
j += 1
12+
else:
13+
j += 1
14+
return i

leetcode/461.汉明距离.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 异或,其二进制同0异1,再将十进制的异或结果转为二进制,求1的个数
2+
class Solution(object):
3+
def hammingDistance(self, x, y):
4+
return bin(x^y).count('1')

0 commit comments

Comments
 (0)