LeetCode_91、120两题(动态规划)
2017-05-13 15:30
337 查看
LeetCode_91. Decode Ways
原题:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message “12”, it could be decoded as “AB” (1 2) or “L” (12).
The number of ways decoding “12” is 2.
解析:让我们判断字符串里有多少种不同的字母组合,很典型的动态规划题。考虑第i个元素,有以下几种情况:
1、s[i]为0,s[i-1]为1和2,显然最后两个只能是s[i]和s[i-1]组合的元素,因此到第i个元素的种类a[i] = a[i-2](当然s[i-1]不为1和2则没有任何组合的可能性,因为30、40这些不构成字母,单独的0也不能构成字母)。
2、s[i]不为0,s[i-1]为1,或者s[i]为1~6,s[i-1]为2,则最后两个元素可以组合成一个字母,也可以分别成两个字母,则此时a[i] = a[i-1] + a[i-2]
3、剩下除了类别1、2之外的,该种情况,只能将最后一个元素单独成为一个元素,此时a[i] = a[i-1]
情况分好后,注意对初始值的分析与赋值即可。
具体代码如下:
LeetCode_120. Triangle
原题:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
—- [2],
— [3,4],
– [6,5,7],
- [4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
解析:就是从顶部到底部的最小和,注意每行之间取值要相邻。
一开始,单纯以为直接用动态规划,从顶到下依次求最小值就行了,但是知道了到最底部,由于每个值不一样,可能最小值就会发生改变。
于是我换了一种从底部到顶部的做法,还是用动态规划,对每一行第i个元素,若以它为最后一个元素的最小和
//此时minsum是与它相邻下一行的两个元素部分的最小和
minsum[i] = 该元素的值 + min(minsum[i],minsum[i+1])
具体代码如下:
原题:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message “12”, it could be decoded as “AB” (1 2) or “L” (12).
The number of ways decoding “12” is 2.
解析:让我们判断字符串里有多少种不同的字母组合,很典型的动态规划题。考虑第i个元素,有以下几种情况:
1、s[i]为0,s[i-1]为1和2,显然最后两个只能是s[i]和s[i-1]组合的元素,因此到第i个元素的种类a[i] = a[i-2](当然s[i-1]不为1和2则没有任何组合的可能性,因为30、40这些不构成字母,单独的0也不能构成字母)。
2、s[i]不为0,s[i-1]为1,或者s[i]为1~6,s[i-1]为2,则最后两个元素可以组合成一个字母,也可以分别成两个字母,则此时a[i] = a[i-1] + a[i-2]
3、剩下除了类别1、2之外的,该种情况,只能将最后一个元素单独成为一个元素,此时a[i] = a[i-1]
情况分好后,注意对初始值的分析与赋值即可。
具体代码如下:
class Solution { public: int numDecodings(string s) { if(s.empty())return 0; int size = s.size(); int a[10000]; if(s[0] == '0')return 0; if(size == 1){ return 1; } a[0] = 1; if((s[0] == '1' && s[1] >= '1' && s[1] <= '9') || (s[0] == '2' && s[1] >= '1' && s[1] <= '6'))a[1] = 2; else if(s[1] == '0' && (s[0] >= '3' && s[0] <= '9'))return 0; else a[1] = 1; for(int i = 2; i < size; i++){ if(s[i] == '0'){ if(s[i - 1] == '1' || s[i - 1] == '2')a[i] = a[i - 2]; else return 0; } else if(s[i - 1] == '1' || (s[i - 1] == '2' && s[i] >= '1' && s[i] <= '6'))a[i] = a[i - 1] + a[i - 2]; else a[i] = a[i - 1]; } return a[size - 1]; } };
LeetCode_120. Triangle
原题:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
—- [2],
— [3,4],
– [6,5,7],
- [4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
解析:就是从顶部到底部的最小和,注意每行之间取值要相邻。
一开始,单纯以为直接用动态规划,从顶到下依次求最小值就行了,但是知道了到最底部,由于每个值不一样,可能最小值就会发生改变。
于是我换了一种从底部到顶部的做法,还是用动态规划,对每一行第i个元素,若以它为最后一个元素的最小和
//此时minsum是与它相邻下一行的两个元素部分的最小和
minsum[i] = 该元素的值 + min(minsum[i],minsum[i+1])
具体代码如下:
class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { if(triangle.size() == 1)return triangle[0][0]; int size = triangle.size(); vector<int> minsum(triangle[size - 1]); for(int i = size - 2; i >= 0 ; i --){ for(int j = 0; j < triangle[i].size() ; j ++){ minsum[j] = triangle[i][j] + min(minsum[j],minsum[j+1]); } } return minsum[0]; } };
相关文章推荐
- leetcode_[python/C++]_91_Decode Ways_动态规划
- LeetCode-91. Decode Ways
- 【leetcode】【91】Decode Ways
- 【Leetcode 动态规划】 买卖股票 I II III IV 冷却,共5题
- Leetcode 动态规划 Candy
- Leetcode 动态规划 Unique Paths
- LeetCode 72 Edit Distance(动态规划)
- LeetCode 123. Best Time to Buy and Sell Stock III(动态规划)
- LeetCode(97) Interleaving String(动态规划)
- leetcode:Longest Valid Parentheses 使用动态规划O(n)思路
- leetcode 300. Longest Increasing Subsequence-最长子序列|动态规划
- leetcode 337. House Robber III-动态规划|Java|Python简洁高效
- leetcode-53-Maximum Subarray 动态规划
- 【leetcode】97. Interleaving String【java】动态规划,并有详细解释
- 【LeetCode-动态规划】Climbing Stairs【较重要:含dp解题思路套路】
- [算法作业-动态规划][LeetCode] 97. Interleaving String
- Leetcode解题笔记 70. Climbing Stairs [Easy] 动态规划
- Leetcode 动态规划 198HouseRobbingI+II +746Min Cost Climbing Stairs
- 动态规划 DP leetcode403 青蛙过河问题
- leetcode || 91、Decode Ways