44. Decode Ways && Gray Code
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的更多相关文章
- URAL 1780 G - Gray Code 找规律
G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- Decode Ways leetcode java
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- Ural 1780 Gray Code 乱搞暴力
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- Android 开源组件 ----- Android LoopView无限自动轮转控件
Android 开源组件 ----- Android LoopView无限自动轮转控件 2015-12-28 15:26 by 杰瑞教育, 32 阅读, 0 评论, 收藏, 编辑 一.组件介绍 App ...
- JS的(function($){})(query)
function(arg){...} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的,由于操作符的优先级,函数本身也需要用括号,即:(function(arg){.. ...
- VMware安装虚拟系统问题
问题1: 我装了vmware 6.5.2,用它来安装深度的 GhostXP 时,在分区之后重启出现Invalid system disk,Replace the disk and then press ...
- Reverse-Daily(2)-wow
链接:http://pan.baidu.com/s/1eS9JNP4 密码:ltl4 本体分析比较简单,算法是解一个22元一次方程 这里引入了numpy这样一个python库,灰常强大 import ...
- BZOJ 2763 分层图最短路
突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...
- ubuntu下mysql的常用命令,MySQL数据库的基本操作命令
一.mysql服务操作 0.查看数据库版本 sql-> status; 1.net start mysql //启动mysql服务 2.net stop mysql //停止mysql服务 3 ...
- find查找命令的各种使用方法
find是文件查找工具,实时查找,速度慢,精确匹配 find命令基本使用格式 find [options] [查找路径] [查找条件] [处理动作] 查找路径:默认为当前目录 查找条件:默认为查找指定 ...
- Objective-c——UI基础开发第十天(自动布局)
一.autoresizing 的使用(了解) 只能参照父控件 1.实现横竖屏幕切换,不能把控件的frame血丝,需要进行屏幕适配 2.需要参照父控件 use auto layout禁用 才会出现aut ...
- word转pdf字体格式变乱的问题
完成word转pdf的功能之后,本地测试没问题,然后发布到服务器上,就遇到了字体变乱的问题,如下: 由于我本地发布后导出没有出现同样情况,而服务器和本地的最大区别在于字体库,于是,把服务器上关于需要用 ...
- 关于 BCSCTL1 = CALBC1_12MHZ;DCOCTL = CALDCO_12MHZ; 的疑问
/************************************************************ * Calibration Data in Info Mem ******* ...