Skip to content

Commit b9f62f7

Browse files
authored
Add files via upload
1 parent 5302471 commit b9f62f7

8 files changed

+167
-0
lines changed

leetcode/060.第k个排列.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 回溯法,先算出全部全排列,再取第k个
2+
class Solution(object):
3+
def getPermutation(self, n, k):
4+
def gen(output, nums):
5+
if not nums:
6+
res.append(output)
7+
for i in range(len(nums)):
8+
gen(output + str(nums[i]), nums[:i] + nums[i+1:])
9+
10+
res = []
11+
nums = [i for i in range(1, n+1)]
12+
gen('', nums)
13+
return res[k-1]
14+
15+
16+
# 找规律,一位一位地组合数字
17+
import math
18+
class Solution(object):
19+
def getPermutation(self, n, k):
20+
nums = [str(i) for i in range(1, n+1)]
21+
res = ''
22+
n -= 1
23+
while n >= 0:
24+
# n个数,以每一位数字开头有(n-1)!个排列
25+
fac_n = math.factorial(n)
26+
27+
# 确定n个数中第k个排列的第一位数的位置,向上取整再减1,该行等于以下四行
28+
loc = math.ceil(k / fac_n) - 1
29+
# if k % fac_n:
30+
# loc = k // fac_n
31+
# else:
32+
# loc = k // fac_n - 1
33+
34+
res += nums[loc]
35+
nums.pop(loc)
36+
# 确定了前一位数后,k减去前面已排除的多个(n-1)!全排列
37+
k %= fac_n
38+
n -= 1
39+
return res

leetcode/223.矩形面积.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def computeArea(self, A, B, C, D, E, F, G, H):
3+
# 两个矩形的面积
4+
a = (C-A) * (D-B)
5+
b = (G-E) * (H-F)
6+
# 重叠部分的面积
7+
x = max(0, min(C, G) - max(A, E))
8+
y = max(0, min(D, H) - max(B, F))
9+
area = a+b-x*y
10+
return area

leetcode/390.消除游戏.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 按题目说明解法
2+
class Solution(object):
3+
def lastRemaining(self, n):
4+
nums = [i+1 for i in range(n)]
5+
res = []
6+
while len(nums) > 1:
7+
for i in range(1, len(nums), 2):
8+
res.append(nums[i])
9+
nums, res = res[::-1], []
10+
return nums[0]
11+
12+
13+
# 找规律,如果输入a输出b,则输入2a输出2*(a-b+1)
14+
class Solution(object):
15+
def lastRemaining(self, n):
16+
if n == 1:
17+
return 1
18+
return 2 * (n/2 - self.lastRemaining(n/2) + 1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 分治法
2+
class Solution(object):
3+
def longestSubstring(self, s, k):
4+
if not s:
5+
return 0
6+
# 遍历字符串去重后的字符
7+
for c in set(s):
8+
# 如果字符个数少于k
9+
if s.count(c) < k:
10+
# 按照该字符拆分字符串,再递归求出拆分后的字串中最长的字串
11+
return max(self.longestSubstring(t, k) for t in s.split(c))
12+
# 每个字符的个数都大于等于k,则返回字符串的长度
13+
return len(s)

leetcode/419.甲板上的战舰.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 遍历时如果为'X',并且其左边和上边不为'X',则个数加1
2+
class Solution(object):
3+
def countBattleships(self, board):
4+
res = 0
5+
for i in range(len(board)):
6+
for j in range(len(board[0])):
7+
if board[i][j] == 'X':
8+
if (i > 0 and board[i-1][j] == 'X') or (j > 0 and board[i][j-1] == 'X'):
9+
continue
10+
res += 1
11+
return res

leetcode/473.火柴拼正方形.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 递归,贪心,深度优先,剪枝
2+
# 每次尝试把每根火柴拼到4条边,全部拼完则可以组成正方形
3+
class Solution(object):
4+
def makesquare(self, nums):
5+
# 不能组成正方形的情况:1.火柴总长度不是4的倍数 2.火柴数少于4 3.最长的火柴大于目标边长
6+
sums = sum(nums)
7+
if sums % 4 or len(nums) < 4:
8+
return False
9+
# 贪心,先排序可以节约回溯的次数
10+
nums.sort(reverse=True)
11+
side_len = sums / 4
12+
if nums[0] > side_len:
13+
return False
14+
15+
def dfs(i, sides):
16+
"""
17+
深度优先遍历
18+
:param i: 当前访问的火柴的位置
19+
:param sides: 当前4条边的长度
20+
:return:
21+
"""
22+
# 递归终止条件,火柴全部拼完
23+
if i == len(nums):
24+
return True
25+
# 尝试把火柴拼到4条边中
26+
for j in range(4):
27+
# 当前边长加上新的火柴后长度小于等于目标边长。当前边长与前一边长相等时可跳过,以为如果前一边长失败,当前边长也会失败
28+
if sides[j] + nums[i] <= side_len and (j == 0 or sides[j] != sides[j - 1]):
29+
# 放入当前火柴
30+
sides[j] += nums[i]
31+
# 放入下一根火柴
32+
if dfs(i + 1, sides):
33+
return True
34+
# 放入失败,取出火柴准备放入下一条边
35+
sides[j] -= nums[i]
36+
# 四条边都不能放火柴,回溯
37+
return False
38+
# 初始化边并从0开始放入
39+
return dfs(0, [0, 0, 0, 0])

leetcode/695.岛屿的最大面积.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxAreaOfIsland(self, grid):
3+
m, n, maxArea = len(grid), len(grid[0]), 0
4+
5+
# 获取岛屿面积
6+
def getArea(grid, x, y):
7+
# 符合条件的土地,将其置0,并累加四个方向的土地,得到岛屿的面积
8+
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
9+
grid[x][y] = 0
10+
return 1 + getArea(grid, x + 1, y) + getArea(grid, x - 1, y) + getArea(grid, x, y + 1) + getArea(grid, x, y - 1)
11+
return 0
12+
13+
# 遍历每个位置,得到每个位置上的面积并更新最大面积
14+
for i in range(m):
15+
for j in range(n):
16+
area = getArea(grid, i, j)
17+
maxArea = area if area > maxArea else maxArea
18+
return maxArea

leetcode/733.图像渲染.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def floodFill(self, image, sr, sc, newColor):
3+
d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
4+
m, n, oldColor = len(image), len(image[0]), image[sr][sc]
5+
6+
# 新值等于就值则返回原图像,避免无限搜索
7+
if newColor == oldColor:
8+
return image
9+
10+
# 将符合条件的位置赋新值,再搜索该位置的四个方向是否符合
11+
def dfs(x, y):
12+
image[x][y] = newColor
13+
for dx, dy in d:
14+
nx, ny = x + dx, y + dy
15+
if 0 <= nx < m and 0 <= ny < n and image[nx][ny] == oldColor:
16+
dfs(nx, ny)
17+
18+
dfs(sr, sc)
19+
return image

0 commit comments

Comments
 (0)