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.

思想: 动态规划,分析:

1211212:  前面为1 或者 (前面是2 且 当前值 < 7) : f(n) = f(n-1) +f (n-2);

122:  当前值为 0,前面是 1 或 2, f(n) = f(n-2). 否则,返回 0。

其他情况, f(n) = f(n-1);

class Solution {
public:
int numDecodings(string s) {
int n = s.size();
if(n == 0 || s[0] == '0') return 0;
vector<int> f(n+1);
f[0] = f[1] = 1;
for(int index = 2; index <= n; ++index) {
if('0' == s[index-1]) {
if('1' == s[index-2] || '2' == s[index-2]) f[index] = f[index-2];
else return 0;
}
else if('1' == s[index-2] || ( '2' == s[index-2] && s[index-1] < '7')) f[index] = f[index-1] + f[index-2];
else f[index] = f[index-1];
}
return f[n];
}
};

Gray Code

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note: For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

思想: 方法比较巧妙。 每次改变循环一轮,只改变最高位  0 ->1。

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> vec(1, 0);
for(int i = 0; i < n; ++i) {
int v = 1 << i;
for(int j = vec.size()-1; j >= 0; --j)
vec.push_back(vec[j]+v);
}
return vec;
}
};

44. Decode Ways && Gray Code的更多相关文章

  1. URAL 1780 G - Gray Code 找规律

    G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. Decode Ways leetcode java

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  3. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...

  4. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  7. Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  8. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  9. [LintCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

随机推荐

  1. java中Commons-fileupload实现上传

    java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文 ...

  2. Objective-C学习笔记-第一天(1)

    .h头文件,用于声明一些公开的属性.方法.头文件一般不会放太多的东西,没必要暴露太多接口,私有和封装. .m主文件,用于实现.h文件中的方法,以及一些其它的方法. -(返回值)方法名参数1名称:(参数 ...

  3. HDU 5358 尺取法+枚举

    题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...

  4. join()、implode()函数

    join() 函数 join() 函数把数组元素组合为一个字符串. join() 函数是 implode() 函数的别名. 语法 join(separator,array) 参数 描述 separat ...

  5. [GodLove]Wine93 Tarining Round #2

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44704#overview 题目来源: ZOJ Monthly, June 2 ...

  6. watir学习系列--Watir API介绍

    文本框:          <INPUT id="email" name="_fmu.u._0.e" value="" />   ...

  7. REST概念和应用 - TODO

    Motivation Sometimes I fell like giving up, then I remember I have a lot of motherfuckers to prove w ...

  8. Opencv读取与显示图片

    #include "stdafx.h"#include "cv.h"#include "cxcore.h"#include "hi ...

  9. uva 10622

    http://vjudge.net/contest/140673#problem/H 求某个数字(最大到10^9,可为负值)写成完全p次方数的指数p是多少 分析: 先进行唯一分解,之后p整除各个素因子 ...

  10. Git 常用命令整理

    初始化配置 #配置使用git仓库的人员姓名 git config --global user.name "Your Name Comes Here" #配置使用git仓库的人员em ...