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 ...
随机推荐
- ExtJs4中的复选树级联选择
好久没有写新的博文了,过了个年休息了近一个月,人都懒散了.. 这几天要把项目中的几个模块有ext3升级到ext4,还要保持页面展示和功能要跟3.x版本的一样.升级并不是一件简单的是,基本相当于重写了, ...
- realestate.cei.gov.cn
using AnfleCrawler.Common; using System; using System.Collections.Concurrent; using System.Collectio ...
- 判断CAD图纸版本
判断CAD图纸版本Dwg文件版本 使用记事本打开DWG图纸文件,在最开始有6个字母和数字组合,即为图纸的版本号 AC1015:CAD2000版本: AC1018:CAD2004版本: AC1021:C ...
- 如何在Eclipse中设置默认的JSP文件头部编码
如何在Eclipse中设置默认的JSP文件头部编码 一般,我们为了以后在导入和导出程序的时候(特别是项目较大,文件多)一般都默认文件编码格式为UTF-8 如果你通常都是通过Eclipse来编写程序,那 ...
- UIkit框架之UIcollection
1.继承链:UIScrollView:UIview:UIResponder:NSObject 2.collection view使用的数据源需要遵守UICollectionViewDataSource ...
- Libgdx 开发指南(1.2) 应用框架——模块概览
模块概览 引言 LibGDX由一些为一个典型游戏架构中的各个步骤提供服务的模块组成. Input:为所有平台提供一致的输入模型与处理器.支持键盘.触屏.加速度传感器与鼠标. Graphics:使用硬件 ...
- C++ Daily 《3》----构造函数可否是虚函数
C++ 中构造函数可否是虚函数? 绝不要!! 而且,在构造函数中调用虚函数也是不提倡的行为,因为会引发预想不到的结果. 因为,在 derived class 对象构造的过程中,首先调用的是基类的构造函 ...
- C# 代码示例_结构/数组/枚举...
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- ArrayBlockingQueue-我们到底能走多远系列(42)
我们到底能走多远系列(42) 扯淡: 乘着有空,读些juc的源码学习下.后续把juc大致走一边,反正以后肯定要再来. 主题: BlockingQueue 是什么 A java.util.Queue t ...
- PHP7在linux下的安装步骤
安装mcrypt: yum install -y php-mcrypt libmcrypt libmcrypt-devel 升级bison: cd /var/soft/ wget http://ftp ...