Decode Ways
来源:互联网 发布:java工程师工作职责 编辑:程序博客网 时间:2024/05/18 11:45
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.
动态规划的一题。从题目意思中,我们可以知道每次编码的长度为1或2.。这样其实就像是跳台阶,每次能跳一步
或者两步。
class Solution {public: bool check(char n){return n>='1'&&n<='9';} bool check(char a, char b){return a=='1'||(a=='2'&&(b<='6'&&b>='0'));} int numDecodings(string s) { int len = s.size(); if(len == 0) return 0; if(len == 1) return check(s[0]); int f[len]; memset(f,0,sizeof(int)*len); f[0] = check(s[0]); f[1] = check(s[0],s[1])+(check(s[0])&check(s[1])); for(int i = 2;i<len;i++) { if(check(s[i])) f[i] = f[i]+f[i-1]; if(check(s[i-1],s[i])) f[i] = f[i]+f[i-2]; } return f[len - 1]; }};
0 0
- 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
- Ubuntu 12.04上PPPoe服务的搭建和使用
- 套接字编程API
- 关于opencv2.4.9配置vS2010(win7 32)
- makefile.new(7117) : error U1087: cannot have : and :: dependents for same target
- sql 创建异库链接
- Decode Ways
- CoffeeScript的缩进
- NGUI技能冷却CD特效制作
- Linux 命令整理 —— 基本操作
- 对于面向接口编程的理解
- ScheduledExecutorService 多线程类简单使用
- MRC注意事项
- ContentProvider与ContentResolver
- Choosing an OpenGL API Version(选择OpenGL API版本)