[LintCode] 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, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2nintegers.
Notice
For a given n, a gray code sequence is not uniquely defined.
[0,2,3,1] is also a valid gray code sequence according to the above definition.
Given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
O(2n) time.
LeetCode上的原题,请参见我之前的博客Gray Code。
解法一:
class Solution {
public:
/**
* @param n a number
* @return Gray code
*/
vector<int> grayCode(int n) {
vector<int> res;
for (int i = ; i < pow(, n); ++i) {
res.push_back((i >> ) ^ i);
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param n a number
* @return Gray code
*/
vector<int> grayCode(int n) {
vector<int> res{};
for (int i = ; i < n; ++i) {
int size = res.size();
for (int j = size - ; j >= ; --j) {
res.push_back(res[j] | ( << i));
}
}
return res;
}
};
解法三:
class Solution {
public:
/**
* @param n a number
* @return Gray code
*/
vector<int> grayCode(int n) {
vector<int> res{};
int len = pow(, n);
for (int i = ; i < len; ++i) {
int pre = res.back();
if (i % == ) {
pre = (pre & (len - )) | (~pre & );
} else {
int cnt = , t = pre;
while ((t & ) != ) {
++cnt; t >>= ;
}
if ((pre & ( << cnt)) == ) pre |= ( << cnt);
else pre &= ~( << cnt);
}
res.push_back(pre);
}
return res;
}
};
解法四:
class Solution {
public:
/**
* @param n a number
* @return Gray code
*/
vector<int> grayCode(int n) {
vector<int> res{};
unordered_set<int> s;
stack<int> st;
s.insert();
st.push();
while (!st.empty()) {
int t = st.top(); st.pop();
for (int i = ; i < n; ++i) {
int k = t;
if ((k & ( << i)) == ) k |= ( << i);
else k &= ~( << i);
if (s.count(k)) continue;
s.insert(k);
st.push(k);
res.push_back(k);
break;
}
}
return res;
}
};
解法五:
class Solution {
public:
/**
* @param n a number
* @return Gray code
*/
vector<int> grayCode(int n) {
vector<int> res;
unordered_set<int> s;
helper(n, s, , res);
return res;
}
void helper(int n, set<int>& s, int out, vector<int>& res) {
if (!s.count(out)) {
s.insert(out);
res.push_back(out);
}
for (int i = ; i < n; ++i) {
int t = out;
if ((t & ( << i)) == ) t |= ( << i);
else t &= ~( << i);
if (s.count(t)) continue;
helper(n, s, t, res);
break;
}
}
};
[LintCode] Gray Code 格雷码的更多相关文章
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- 089 Gray Code 格雷编码
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...
- Leetcode89. Gray Code格雷编码
给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- 格雷码(Gray code)仿真
作者:桂. 时间:2018-05-12 16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码, ...
随机推荐
- RTP timestamp与帧率及时钟频率的关系
转自:http://blog.csdn.net/jasonhwang/article/details/7316128 RTP timestamp是用时钟频率(clock rate)计算而来表示时间的. ...
- 信号量互斥,王明学learn
信号量互斥 信号量(又名:信号灯)与其他进程间通信方式不大相同,主要用途是保护临界资源(进程互斥).进程可以根据它判定是否能够访问某些共享资源.除了用于访问控制外,还可用于进程同步. 一.信号量分类 ...
- 遍历List过程中删除元素的正确做法(转)
遍历List过程中删除元素的正确做法 public class ListRemoveTest { 3 public static void main(String[] args) { 4 ...
- 处理FF margin-top下降问题
处理DIV子级ZImargin-top下降,父级更着下降问题 html结构如下 <div id="top"> <div id="zi"> ...
- android中随着ScrollView的滑动,titleBar状态的改变
今天项目有一个需求,,类是于QQ空间里面的一个功能,于是就研究了一下,嗯,说这么多,可能还有人不知道指的是那个,直接上效果图.见谅,不会弄动态图: 对,就是这种效果,我研究了一下,思路如下: 1. ...
- python 多重继承
多重继承 除了从一个父类继承外,Python允许从多个父类继承,称为多重继承. 多重继承的继承链就不是一棵树了,它像这样: class A(object): def __init__(self, a) ...
- Windows64 系统下Python、NumPy与matplotlib 安装方法
今下午想用Python跑RNN网络,结果代码在导入包numpy时并没有报错,但是在用里面的函数时报错,因小编也是新手,只学习了Python的基础语法,并没有使用过第三方包,安装了一下午还没弄好,本以为 ...
- node express新项目默认主文件app.js
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon') ...
- 对于div的右浮动会导致顺序会改变
当我们设置几个div右浮动的时候会出现顺序的改变,直接倒序了. 解决的方法是在几个div外面加上一个大的div即可,但是里面的所有div都要左浮动才行,具体做法如下: <!DOCTYPE htm ...
- MVC Autofac 注入点
关键句: ** DependencyResolver.SetResolver(new AutofacDependencyResolver(container));** protected overri ...