91. Decode Ways

网友投稿 494 2022-11-11

91. Decode Ways

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.

思路:动态规划。

class Solution { public int numDecodings(String s) { if (s == null || s.length() == 0) return 0; char[] sa = s.toCharArray(); int[] nums = new int[sa.length+1]; nums[0] = 1; for(int i=1; i<=sa.length; i++) { if (sa[i-1] != '0') nums[i] += nums[i-1]; if (i>1 && sa[i-2] == '1') nums[i] += nums[i-2]; if (i>1 && sa[i-2] == '2' && sa[i-1] >= '0' && sa[i-1] <= '6') nums[i] += nums[i-2]; } return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:783. Minimum Distance Between BST Nodes
下一篇:maven配置文件pom增加变量取版本号方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~