Decode Ways
来源:互联网 发布:可控核聚变 知乎 编辑:程序博客网 时间:2024/05/10 00:34
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.
public class Solution { public int numDecodings(String s) {// Start typing your Java solution below// DO NOT write main() functionint n = s.length();if (n == 0)return 0;int[] res = new int[n + 1];res[0] = 1;if (s.charAt(0) != '0')res[1] = 1;String temp;for (int i = 1; i < n; ++i) {temp = s.substring(i - 1, i + 1);int value = Integer.parseInt(temp);if (s.charAt(i) != '0')res[i + 1] = res[i];if (value > 9 && value < 27) {res[i + 1] += res[i - 1];}}return res[n];}}
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode ways
- Decode Ways
- Decode Ways
- Decode Ways
- Decode Ways
- Leetcode: Gray Code
- 01-超链接文字的样式(页面效果)
- Configuring the Apache MPM on Ubuntu
- dismissModalViewControllerAnimated不起作用
- 02-网页换肤(页面效果)
- Decode Ways
- self dismissModalViewControllerAnimated:YES 无效
- ubuntu下取消mysql和apache开机启动
- InstallShiled 2012完全卸载-删除文件夹和子文件夹和文件
- InstallShield 2012 升级程序制作时的product code 和upgrade code
- 03-文字滚动(页面效果)
- sereral common used sh command
- GTK+第一课, 简单窗口创建
- vbscript删除母文件夹