Bilibili算法Coding
import sys
n = sys.stdin.readline().strip()
# arr = list(map(int, sys.stdin.readline().strip().split(" ")))
class Solution:
def numDecodingstrs(self, strs):
if not strs or strs.startswith("0"):
return 0
if len(strs) == 1 and strs[0] != "0":
return 1
length = len(strs)
dp = [0 for _ in range(length + 1)]
dp[0] = 1 if strs[0] != "0" else 0
dp[1] = 1 if strs[1] != "0" else 0
for i in range(2, length + 1):
if 1 <= int(strs[i-1]) <= 9:
dp[i] += dp[i-1]
if 10 <= int(strs[i-2:i]) <= 26:
dp[i]+= dp[i-2]
return dp[-1]
print(Solution().numDecodingstrs(n))
第二题
import sys
h, w = list(map(int, sys.stdin.readline().strip().split(" ")))
res = []
for i in range(h):
res.append(list(map(int, sys.stdin.readline().strip().split(" "))))
m = int(sys.stdin.readline().strip())
filter = []
for i in range(m):
filter.append(list(map(float, sys.stdin.readline().strip().split(" "))))
# print(filter)
# print(res)
class Solution:
def getSquare(self, filter, arr):
res = 0
for i in range(len(filter)):
for j in range(len(filter[0])):
res += filter[i][j] * arr[i][j]
return int(res)
def getAll(self, res, filter, h, w, m):
h_next = h - m + 1
w_next = w - m + 1
outs = []
for i in range(h_next):
temp = []
for j in range(w_next):
arr = [[0] * m for _ in range(m)]
for x in range(m):
for y in range(m):
arr[x][y] = res[i+x][j+y]
# print(arr[x][y])
temp.append(str(self.getSquare(filter, arr)))
outs.append(temp)
return outs
outs = Solution().getAll(res, filter, h, w, m)
for i in outs:
print(" ".join(i))
第三题
import sys
strs = sys.stdin.readline().strip()
arr = strs.split(" ")
out = []
for i in arr:
if len(i) & 1 == 1:
out.append(i[::-1])
else:
out.append(i)
print(" ".join(out))
查看25道真题和解析