第一题----------------------------------------------------
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if text1[i - 1] == text2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]
第二题-----------------------------------------------------
class Solution:
def maxSubArray(self, nums):
n = len(nums)
if n == 1:
return nums[0]
max = -10001
nums_sum = 0
for i in range(len(nums)):
nums_sum = nums_sum + nums[i]
if max < nums_sum:
max = nums_sum
if nums_sum <= 0:
nums_sum = 0
return max
第三题-----------------------------------
class Solution:
def readBinaryWatch(self, turnedOn: int) -> List[str]:
ans = list()
for h in range(12):
for m in range(60):
if bin(h).count("1") + bin(m).count("1") == turnedOn:
ans.append(f"{h}:{m:02d}")
return ans
算法4.26
最新推荐文章于 2024-09-27 16:14:00 发布