零钱兑换
解法1:贪心+剪枝
(但是该解法会超时)
解法2:动态规划
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for coin in coins:
for x in range(coin, amount + 1):
dp[x] = min(dp[x], dp[x - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
Excel表列序号
解法:26进制转换
通过题意可知,该问题可以转换为一个26进制数转换问题。
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for i, char in enumerate(columnTitle[::-1]):
result += (ord(char) - ord('A') + 1) * 26**i
# print(result)
return result