Skip to content

Commit 1357b0d

Browse files
authored
Add files via upload
1 parent d0d434d commit 1357b0d

16 files changed

+255
-0
lines changed

leetcode/011.盛最多水的容器.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 双指针移动
2+
class Solution(object):
3+
def maxArea(self, height):
4+
l, r, m = 0, len(height)-1, 0
5+
while l < r:
6+
# 记录当前最大面积
7+
hl, hr = height[l], height[r]
8+
cur = min(hl, hr) * (r-l)
9+
m = max(m, cur)
10+
# 高度较小一端向内移动,获得可能更大的面积
11+
if hl < hr:
12+
l += 1
13+
else:
14+
r -= 1
15+
return m

leetcode/015.三数之和.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 数组排序后,先固定一个数,再用双指针从头尾向内移动确定另外两个数
2+
class Solution(object):
3+
def threeSum(self, nums):
4+
n = len(nums)
5+
nums.sort()
6+
res = set()
7+
for i in range(n-2):
8+
x, y = i+1, n-1
9+
while x < y:
10+
if nums[i] + nums[x] + nums[y] == 0:
11+
# 去重
12+
res.add((nums[i], nums[x], nums[y]))
13+
x += 1
14+
y -= 1
15+
if nums[i] + nums[x] + nums[y] > 0:
16+
y -= 1
17+
if nums[i] + nums[x] + nums[y] < 0:
18+
x += 1
19+
# 还原为列表
20+
return [list(l) for l in res]

leetcode/018.四数之和.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 数组排序后,先固定两个数,再用双指针从头尾向内移动确定另外两个数
2+
class Solution(object):
3+
def fourSum(self, nums, target):
4+
n = len(nums)
5+
nums.sort()
6+
res = set()
7+
for i in range(n-3):
8+
for j in range(i+1, n-2):
9+
l, r = j+1, n-1
10+
while l < r:
11+
temp = nums[i] + nums[j] + nums[l] + nums[r]
12+
if temp == target:
13+
# 去重
14+
res.add((nums[i], nums[j], nums[l], nums[r]))
15+
l += 1
16+
r -= 1
17+
# 结果比目标值大,右指针左移减小数值
18+
if temp > target:
19+
r -= 1
20+
# 结果比目标值小,左指针右移增大数值
21+
if temp < target:
22+
l += 1
23+
return [list(t) for t in res]

leetcode/043.字符串相乘.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 multiply(self, num1, num2):
3+
return str(int(num1)*int(num2))

leetcode/050.Pow(x, n).py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 折半计算,每次将n缩小一半
2+
class Solution(object):
3+
def myPow(self, x, n):
4+
if n < 0:
5+
n, x = abs(n), 1/x
6+
res = 1
7+
while n:
8+
# n为奇数则需要乘一个x
9+
if n % 2:
10+
res *= x
11+
# x翻倍
12+
x *= x
13+
# n缩小一半
14+
n //= 2
15+
return res
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 按空格切分单词再反转
2+
class Solution(object):
3+
def reverseWords(self, s):
4+
return ' '.join(s.split()[::-1])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 遍历数组,用一个长度为k的列表存放最大的k个数,返回列表中的最小值即为第k大的数
2+
class Solution(object):
3+
def findKthLargest(self, nums, k):
4+
res = []
5+
for num in nums:
6+
if len(res) < k:
7+
res.append(num)
8+
elif num > min(res):
9+
res[res.index(min(res))] = num
10+
return min(res)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 方法一:分类讨论
2+
class Solution(object):
3+
def productExceptSelf(self, nums):
4+
z = nums.count(0)
5+
# 大于1个0的情况,累积结果全部为0
6+
if z > 1:
7+
return [0]*len(nums)
8+
# 1个0的情况,除了0位置,其他都为0
9+
if z == 1:
10+
res = [0]*len(nums)
11+
index = nums.index(0)
12+
nums.pop(index)
13+
r = 1
14+
for num in nums:
15+
r *= num
16+
res[index] = r
17+
return res
18+
# 没有0的情况,先算累积,再用总积去除逐个位置的数
19+
res = 1
20+
for num in nums:
21+
res *= num
22+
return [res/i for i in nums]
23+
24+
25+
# 方法二:当前数 = 左边的乘积 x 右边的乘积
26+
class Solution(object):
27+
def productExceptSelf(self, nums):
28+
n = len(nums)
29+
l, r, res = 1, 1, [1]*n
30+
for i in range(n):
31+
res[i] *= l
32+
l *= nums[i]
33+
res[n-1-i] *= r
34+
r *= nums[n-1-i]
35+
return res

leetcode/376.摆动序列.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 贪心算法
2+
class Solution(object):
3+
def wiggleMaxLength(self, nums):
4+
# 边界条件
5+
n = len(nums)
6+
if n < 2:
7+
return n
8+
9+
# 以正/负结尾时的最长摆动序列长度
10+
up = down = 1
11+
# 正负性相同则长度不变,相反则加1
12+
for i in range(1, n):
13+
if nums[i-1] < nums[i]:
14+
up = down + 1
15+
if nums[i-1] > nums[i]:
16+
down = up + 1
17+
return max(up, down)

leetcode/402.移掉K位数字.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 利用栈弹出元素,使前面的数字尽可能小
2+
class Solution(object):
3+
def removeKdigits(self, num, k):
4+
res = []
5+
for n in num:
6+
while res:
7+
if not k or n >= res[-1]:
8+
res.append(n)
9+
break
10+
else:
11+
res.pop()
12+
k -= 1
13+
else:
14+
if n != '0':
15+
res.append(n)
16+
res = res[:len(res)-k]
17+
return ''.join(res) if res else '0'

0 commit comments

Comments
 (0)