文章目录
一,接雨水
1,程序简介
- 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
- 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
- 输出:6
- 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
- 输入:height = [4,2,0,3,2,5]
- 输出:9
提示:
- n = = h e i g h t . l e n g t h n == height.length n==height.length
- 0 < = n < = 3 ∗ 1 0 4 0 <= n <= 3 * 10^4 0<=n<=3∗104
- 0 < = h e i g h t [ i ] < = 1 0 5 0 <= height[i] <= 10^5 0<=height[i]<=105
以下程序实现了这一功能,请你填补空白处内容:
class Solution(object):
def trap(self, height):
ls = len(height)
if ls == 0:
return 0
res, left = 0, 0
while left < ls and height[left] == 0:
left += 1
pos = left + 1
while pos < ls:
if height[pos] >= height[left]:
res += self.rain_water(height, left, pos)
left = pos
pos += 1
elif pos == ls - 1:
max_value, max_index = 0, pos
for index in range(left + 1, ls):
if height[index] > max_value:
max_value = height[index]
max_index = index
res += self.rain_water(height, left, max_index)
left = max_index
pos = left + 1
else:
pos += 1
return res
def rain_water(self, height, start, end):
if end - start <= 1:
return 0
min_m = min(height[start], height[end])
_________________________;
step = 0
for index in range(start + 1, end):
if height[index] > 0:
step += height[index]
return res - step
if __name__ == '__main__':
s = Solution()
print (s.trap([2,6,3,8,2,7,2,5,0]))
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 5 22:31:34 2022
Function: 接雨水
@author: 小梁aixj
"""
class Solution(object):
def trap(self, height):
ls = len(height)
if ls == 0:
return 0
res, left = 0, 0
while left < ls and height[left] == 0:
left += 1
pos = left + 1
while pos < ls:
if height[pos] >= height[left]:
res += self.rain_water(height, left, pos)
left = pos
pos += 1
elif pos == ls - 1:
max_value, max_index = 0, pos
for index in range(left + 1, ls):
if height[index] > max_value:
max_value = height[index]
max_index = index
res += self.rain_water(height, left, max_index)
left = max_index
pos = left + 1
else:
pos += 1
return res
def rain_water(self, height, start, end):
if end - start <= 1:
return 0
min_m = min(height[start], height[end])
res = min_m * (end - start - 1)
step = 0
for index in range(start + 1, end):
if height[index] > 0:
step += height[index]
return res - step
if __name__ == '__main__':
s = Solution()
print (s.trap([0,1,0,2,1,0,1,3,2,1,2,1]))
3,运行结果
二,外观数列
1,程序简介
-
给定一个正整数 n ,输出外观数列的第 n 项。
-
「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
你可以将其视作是由递归公式定义的数字字符串序列:
- countAndSay(1) = “1”
- countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
第一项是数字 1
描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 “11”
描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 “21”
描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 “1211”
描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 “111221”
要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。
例如,数字字符串 “3322251” 的描述如下图:
示例 1:
- 输入:n = 1
- 输出:“1”
- 解释:这是一个基本样例。
示例 2:
- 输入:n = 4
- 输出:“1211”
- 解释:
countAndSay(1) = “1”
countAndSay(2) = 读 “1” = 一 个 1 = “11”
countAndSay(3) = 读 “11” = 二 个 1 = “21”
countAndSay(4) = 读 “21” = 一 个 2 + 一 个 1 = “12” + “11” = “1211”
提示:
- 1 <= n <= 30
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 5 22:31:57 2022
Function: 外观数列
@author: 小梁aixj
"""
class Solution:
def countAndSay(self, n):
if n == 1:
return '1'
x = '1'
while n > 1:
x = self.count(x)
n -= 1
return x
def count(self, x):
m = list(x)
res = []
m.append(None)
i , j = 0 , 0
while i < len(m) - 1:
j += 1
if m[j] != m[i]:
res += [j - i, m[i]]
i = j
return ''.join(str(s) for s in res)
# %%
s = Solution()
print(s.countAndSay(n = 4))
3,运行结果
三,排列序列
1,程序简介
- 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
- “123”
- “132”
- “213”
- “231”
- “312”
- “321”
- 给定 n 和 k,返回第 k 个排列。
示例 1:
- 输入:n = 3, k = 3
- 输出:“213”
示例 2:
- 输入:n = 4, k = 9
- 输出:“2314”
示例 3:
- 输入:n = 3, k = 1
- 输出:“123”
提示:
- 1 <= n <= 9
- 1 <= k <= n!
以下程序实现了这一功能,请你填补空白处内容:
class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
import math
res = [""]
def generate(s, k):
n = len(s)
if n <= 2:
if k == 2:
res[0] += s[::-1]
else:
res[0] += s
return
step = math.factorial(n - 1)
yu = k % step
if yu == 0:
yu = step
c = k // step - 1
else:
c = k // step
res[0] += s[c]
____________________;
return
s = ""
for i in range(1, n + 1):
s += str(i)
generate(s, k)
return res[0]
if __name__ == '__main__':
s = Solution()
print(s.getPermutation(3, 2))
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 5 22:32:07 2022
Function: 排列序列
@author: 小梁aixj
"""
class Solution(object):
def getPermutation(self, n, k):
import math
res = [""]
def generate(s, k):
n = len(s)
if n <= 2:
if k == 2:
res[0] += s[::-1]
else:
res[0] += s
return
step = math.factorial(n - 1)
yu = k % step
if yu == 0:
yu = step
c = k // step - 1
else:
c = k // step
res[0] += s[c]
generate(s[:c] + s[c + 1:], yu)
return
s = ""
for i in range(1, n + 1):
s += str(i)
generate(s, k)
return res[0]
if __name__ == '__main__':
s = Solution()
print(s.getPermutation(3, 3)