[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中的计数用的是格雷码, ...
随机推荐
- C++Primer快速浏览笔记-类型转换
bool b = 42; // _b is true_ int i = b; // _i has value 1_ i = 3.14; // _i has value 3_ double pi = i ...
- 如何安装Ecshop for linux
下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...
- 在 SQL Server 中查询EXCEL 表中的数据遇到的各种问题
SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\KK.xls";User ID=A ...
- JavaScript案例一:Window弹窗案例
注:火狐可运行,谷歌不可运行(安全级别高) <!DOCTYPE html> <html> <head> <title>JavaScript 弹窗案例&l ...
- JavaScript内置对象(字符串,数组,日期的处理)
Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首 ...
- [技术学习]js正则表达式汇总
一.常用正则表达式关键字 ".":任意字符 "*":任意个数 "+":任意个数,至少一个 "?":0-1个 " ...
- S5中新增的Array方法详细说明
ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...
- zookeeper定时清理log
在zookeeper的目录下新建一个脚本,内容如下(zookeeper bin下面也有zkCleanup.sh脚本,原理一样,都是调用java类) shell_dir=$(cd ")&quo ...
- MongoDB 入门之安装篇
前言:MongoDB 在各 OS 上的安装比较简单,此文章只用来记录,不考虑技术深度. 一.Ubuntu 导入 MongoDB 公钥,添该软件源文件,更新源列表 sudo apt-key adv -- ...
- uva10106(大数乘法)
public class Product { public static void main(String[] args){ Scanner sc = new Scanner(new Buffered ...