动态规划——Decode Ways[LeetCode]
来源:互联网 发布:手机怎么查看淘宝积分 编辑:程序博客网 时间:2024/06/06 06:31
题目链接:https://leetcode.com/problems/decode-ways/description/
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.
参考链接:http://blog.csdn.net/linhuanmars/article/details/24570759
dp[i]表示字符串数组前i个字符(即arr[0...i-1])解码方法数;下面代码for循环是以当arr[i]考虑的,它对应的结果为dp[i+1]
public int numDecodings(String s){if(s==null||s.length()==0 || s.charAt(0)=='0')return 0;int[] dp=new int[s.length()+1];dp[0]=1;char[] str=s.toCharArray();if(str[0]=='0')dp[1]=0;elsedp[1]=1;for(int i=1;i<s.length();i++){ if(str[i]=='0'){ //当前字符为0if(str[i-1]=='1'||str[i-1]=='2') //上一个字符为1或2dp[i+1]=dp[i-1];elsedp[i+1]=0;}else{ //当前字符不是0if(str[i-1]=='0'||str[i-1]>='3') //上一个字符为0或3dp[i+1]=dp[i];else if(str[i-1]=='2'&&str[i]>='7'&&str[i]<='9') dp[i+1]=dp[i];else{dp[i+1]=dp[i]+dp[i-1];}}}return dp[s.length()];}
阅读全文
0 0
- 动态规划——Decode Ways[LeetCode]
- Leetcode 动态规划 Decode Ways
- [LeetCode] [动态规划] Decode Ways
- LeetCode Decode Ways(动态规划)
- 13.10—动态规划—Decode Ways
- Decode Ways 动态规划
- 算法学习之动态规划(leetcode 91 Decode Ways)
- leetcode 639. Decode Ways II 动态规划DP
- LeetCode —— Decode Ways
- LeetCode——Decode Ways
- leetcode——Decode Ways
- leetcode笔记—Decode Ways
- decode-ways(动态规划)
- leetcode 091 —— Decode Ways
- Leetcode——91. Decode Ways
- Leetcode——639.Decode Ways II
- 动态规划——数字字符串转换为字母组合的种数(decode-ways)
- 动态规划中级教程91. Decode Ways
- 时间转化+angualr模态框
- lua 工具类1
- python3.6 安装opencv
- 设计模式总纲
- Python 边做边学 8.6 工具类--时间工具(TimeUtil)
- 动态规划——Decode Ways[LeetCode]
- 安装RPM包或者安装源码包
- 在Ubuntu 14.04安装和配置Elasticsearch集群(最新5.5.2版)
- Java知识(泛型)
- 极光推送
- IELTS12 TEST8 allow children to make their own choices on everyday matters
- Scrapy执行crawl命令报错:ModuleNotFoundError: No module named 'win32api'
- POJ 2536 Gopher II(二分图最大匹配)
- hdu 6115 Factory(LCA+暴力)