陕西咸阳建设银行网站对网络营销的理解
1、加一(数组,数学)
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
示例 1:
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
示例 2:
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
示例 3:
输入:digits = [0]
输出:[1]
提示:
- 1 <= digits.length <= 100
- 0 <= digits[i] <= 9
选项代码:
class Solution(object):def plusOne(self, digits):ls = len(digits)for index in reversed(range(ls)):if digits[index] < 9:digits[index] += 1return digitselse:digits[index] = 0digits.insert(0, 1)return digits
# %%
s = Solution()
print(s.plusOne(digits = [1,2,3]))
2、迷宫问题,(需要用递归,图算法)
贡献者:adsls630ef
问题描述:一只老鼠在一个n×n迷宫的入口处,它想要吃迷宫出口处放着奶酪,问这只老鼠能否吃到奶酪?如果可以吃到,请给出一条从入口到奶酪的路径。 思考:解决问题之前,我们首先要做的就是仔细研究问题,找出问题的已知条件和要得到的是什么。和解数学问题、物理问题一样要先弄懂问题。那么,老鼠走迷宫问题的已知条件有什么呢? 数学模型重新定义问题: 问题:问老鼠能否吃到奶酪就是问能否找到一条从迷宫入口到出口的路径。如果不能找到,那么老鼠就吃不到奶酪;如果能够找到,那么就给出这条路径。 观察10×10的迷宫。这个迷宫其实是由10×10=100个格子组成的,其中绿色格子代表墙,白色格子代表路,如图(1)所示。“绿色格子代表墙,白色格子代表路”是用语言形式描述的,需要转换成数学的形式。用1和0分别定义绿色格子和白色格子,可以得到如图(2)的迷宫。 将上面10×10的迷宫定义为如下的二维数组,即 m[10][10]=[1,1,1,0,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,0,0,1, 1,0,1,0,0,0,0,1,0,1, 1,0,1,0,1,1,0,0,0,1, 1,0,0,1,1,0,1,0,1,1, 1,1,1,1,0,0,0,0,1,1, 1,0,0,0,0,1,1,1,0,0, 1,0,1,1,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1] 有了对迷宫的数学定义,就可以很简单的定义迷宫的入口和出口了。迷宫的入口是m[0][3],出口是m[7][9]。老鼠走迷宫问题就是要找一条从入口到出口的路径,如果存在就返回这条路径;如果不存在,就返回不存在这种路径。也就是说,要在二维数组m中找一条从m[0][3]到m[7][9]全部为0的路径。 请使用递归解决迷宫路径查找问题。
以下程序实现了这一功能,请你填补空白处内容:
def maze(m, n, route, pos, export):"""走迷宫m - 迷宫数组,列表n - 迷宫阶数route - 可能的路线,列表pos - 当前位置,元组export - 出口位置,元组"""route.append(pos)if pos == export:print(route)if pos[0] > 0 and m[pos[0]-1][pos[1]] == 0 and (pos[0]-1,pos[1]) not in route:maze(m, n, route[:], (pos[0]-1,pos[1]), export)if pos[0] < n-1 and m[pos[0]+1][pos[1]] == 0 and (pos[0]+1,pos[1]) not in route:maze(m, n, route[:], (pos[0]+1,pos[1]), export)if pos[1] > 0 and m[pos[0]][pos[1]-1] == 0 and (pos[0],pos[1]-1) not in route:maze(m, n, route[:], (pos[0],pos[1]-1), export)________________________________;
m = [[1,1,1,0,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,1,1], [1,0,1,1,1,1,1,0,0,1], [1,0,1,0,0,0,0,1,0,1], [1,0,1,0,1,1,0,0,0,1], [1,0,0,1,1,0,1,0,1,1], [1,1,1,1,0,0,0,0,1,1], [1,0,0,0,0,1,1,1,0,0], [1,0,1,1,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1]
]
maze(m, len(m), list(), (0,3), (7,9))
选项代码:
if pos[1] < n - 1 and m[pos[0]][pos[1] + 1] == 0 and (pos[0], pos[1] + 1) not in route:
maze(m, n, route[:], (pos[0], pos[1] + 1), export)
3、扰乱字符串(字符串,动态规划)
使用下面描述的算法可以扰乱字符串 s 得到字符串 t :
- 如果字符串的长度为 1 ,算法停止
- 如果字符串的长度 > 1 ,执行下述步骤:
- 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 x 和 y ,且满足 s = x + y 。
- 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 s = x + y 或者 s = y + x 。
- 在 x 和 y 这两个子字符串上继续从步骤 1 开始递归执行此算法。
给你两个 长度相等 的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:s1 = "great", s2 = "rgeat"
输出:true
解释:s1 上可能发生的一种情形是:
"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
算法终止,结果字符串和 s2 相同,都是 "rgeat"
这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
示例 2:
输入:s1 = "abcde", s2 = "caebd"
输出:false
示例 3:
输入:s1 = "a", s2 = "a"
输出:true
提示:
- s1.length == s2.length
- 1 <= s1.length <= 30
- s1 和 s2 由小写英文字母组成
以下程序实现了这一功能,请你填补空白处内容:
class Solution(object):def isScramble(self, s1, s2, memo={}):if len(s1) != len(s2) or sorted(s1) != sorted(s2):return Falseif len(s1) <= len(s2) <= 1:return s1 == s2if s1 == s2:return Trueif (s1, s2) in memo:return memo[s1, s2]n = len(s1)for i in range(1, n):___________________________;memo[s1, s2] = Falsereturn False
# %%
s = Solution()
print(s.isScramble(s1 = "great", s2 = "rgeat"))
选项代码:
if not a:b = self.isScramble(s1[:i], s2[-i:], memo) and self.isScramble(s1[i:], s2[:-i], memo)
if a or b:memo[s1, s2] = Truereturn True