题目1:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Example 1:
Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
Example 2:
Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
思路1:
动态规划思路,爬第n个楼梯有F(n) = f(n-1) + f(n-2),因为一次要么爬两级要么一级,此时只和上面两个两个状态有关
反过来:初始状态1层楼梯有1种,2层有2种,第三层则有关系:f(3) = f(1) + f(2), 依次往后退
解答1:
class Solution:
def climbStairs(self, n: int) -> int:
if n <= 1:
return 1
if n == 2:
return 2
s1 = 1
s2 = 2
for i in range(2, n):
temp = s1 + s2
s1 = s2
s2 = temp
return temp
题目2 :
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
思路2:
同上,抢第一个房子有两个结果,抢获得相应钱,不抢,则可以选择抢剩下n-2个房子获得的最多的钱,
此时初始状态为抢f1,1 = M1 或者 不抢f1,0 = 0,第二个房子抢:f2,1 = M2 + f1,0, 不抢 f2,0 = max(f1,1, f1,0),以此类推
抢fn,1 = Mn + fn-1,0, 不抢fn,0 = max(fn-1,1, fn-1,0)
其中:Mn代表第n个房子的钱;fn,1 代表抢第n个房子; fn,0 代表不抢第n个房子
代码2:
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
A = nums[0]
B = 0
for i in range(1, len(nums)):
M = B
B = max(A, B)
A = M + nums[i]
return max(A,B)
题目3:
A message containing letters from A-Z
is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: "12" Output: 2 Explanation: It could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: "226" Output: 3 Exp;lanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
思路3:
依然是动态规划,但是要分情况,分条件:
假设所有的字符之间都是合法的,且两两之间也都可以组成一个字母,此时,转移矩阵为:F(n) = f(n-1) + f(n-2),
初始状态为:l0 = 1, l1=1;
如果连续两个0,则返回0;
如果其中与前一个不能组成一个字母,且不是0,则转移方法为F(n) = f(n-1)
如果其中与前一个不能组成一个字母,或者是0,则转移方法为F(n) = f(n-2)
代码3:
class Solution:
def numDecodings(self, s: str) -> int:
if s == "" or s[0] == "0":
return 0
if len(s) == 1:
return 1
l0 = 1
l1 = 1
l = 1
for i in range(1, len(s)):
if int(s[i-1:i+1])==0 or ((int(s[i-1:i+1])>26 and s[i]=='0')):
return 0
elif int(s[i])==0 :
l = l0
elif int(s[i-1:i+1])>10 and int(s[i-1:i+1])<=26:
l = l0 + l1
elif int(s[i-1:i+1])>26 and int(s[i-1:i+1])<10:
l = l1
l0 = l1
l1 = l
return l