Leetcode Decode Ways
来源:互联网 发布:虚航软件 编辑:程序博客网 时间:2024/06/07 05:55
Leetcode Decode Ways 相关代码。本题是一个典型的dp题,本算法使用cpp实现,如下。
#include <iostream>#include <string>#include <vector>using namespace std;// Every element has two method to resolve.// -- The first is combined with the former one, which need the former cannot combine which// the one before it, and the number of method equals to the str[0:i-2].// -- The second is current one isolated, which means the former one can either combine with// the one before it or not, and the number of method equals to the str[0:i-1].// So, the result is the sum of the two.class Solution {public: int numDecodings(string s) { if (s.length() == 0 || s[0] == '0') { return 0; } int len = s.length(); vector<int> re(len, 0); re[0] = 1; for (int i = 1; i < len; i ++) { // It should not contain continuous '0' if (s[i - 1] == '0' && s[i] == '0') { return 0; } if (s[i - 1] == '1' || (s[i - 1] == '2' && s[i] <= '6')) { if (i >= 2) { re[i] += re[i - 2]; } else { re[i] += 1; } } if (s[i] != '0') { re[i] += re[i - 1]; } } return re[len - 1]; }};int main(int argc, char * argv[]) { Solution so; string test("27"); int re = so.numDecodings(test); cout<<"num:"<<re<<endl; return 0;}
0 0
- Leetcode 91. Decode Ways&&639.Decode ways
- LeetCode: Decode Ways
- [LeetCode]Decode Ways
- LeetCode Decode Ways
- LeetCode : Decode Ways
- [Leetcode] Decode Ways *
- [leetcode] Decode Ways (!!)
- leetcode 17: Decode Ways
- Leetcode: Decode ways
- LeetCode Decode Ways
- [LeetCode]Decode Ways
- [leetcode] Decode Ways
- Leetcode: Decode Ways
- [LeetCode] Decode Ways
- [leetcode]Decode Ways
- LeetCode-Decode Ways
- [leetcode] Decode Ways
- [LeetCode] Decode Ways
- Android调用安卓相机拍照上传
- OS每天15个注意点系列之UI-界面布局
- Android在ListView中获得对应行的button点击事件
- Android中仿QQ聊天内容中提取电话号码,URL
- 安装、配置hive
- Leetcode Decode Ways
- Linux中用stat命令查看文件时3个时间点解析
- Android禁用回退事件
- Difference between a hibernate transaction and a database transaction
- Unity 3DC#语法基本知识点
- eclipse新建项目需改项目包路径时需改动的地方
- 打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹
- Zinnia Installation
- 动态规划-背包问题求解过程【代码 from eason】