【leetcode】Gray Code (middle)
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
思路: Gray Code的意思就是n位的二进制序列,相邻两个只有一位不同。观察
000 - 0
001 - 1
011 - 3
010 - 2
110 - 6
111 - 7
101 - 5
100 - 4
第0位的序列为 01 10 01 这样不断重复
第1位的序列为 0011 1100
第2位的序列为 11110000
这样我们就找到了规律。
我最开始是通过判段每次是第几位变化,通过异或得到新值。 每次第k为变化时满足 i = 2k + n*2(k+1)
vector<int> grayCode(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans;
for(int i = ; i < ( << n); i++)
{
int bit_change = ;
for(int j = ; j < n; j++)
{
if(i % ( << (j + )) - ( << j) == )
{
bit_change = j; break;
}
}
int cur = ( << bit_change);
cur ^= ans.back();
ans.push_back(cur);
}
return ans;
}
后来看其他人的发现更简单的方法
vector<int> grayCode2(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans;
for(int i = ; i < n; i++)
{
int inc = << i;
for(int j = ans.size() - ; j >= ; j--) //每次等第i - 1位正反序都存完毕时,第i位起始为0的情况也存储完了, 只需存储第i位起始为1并且低位倒序输出
{
ans.push_back(ans[j] + inc);
}
}
return ans;
}
【leetcode】Gray Code (middle)的更多相关文章
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【Leetcode】【Medium】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】 Permutation Sequence (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- 大熊君大话NodeJS之------Global Objects全局对象
一,开篇分析 在上个章节中我们学习了NodeJS的基础理论知识,对于这些理论知识来说理解是至关重要的,在后续的章节中,我们会对照着官方文档逐步学习里面的各部分模块,好了该是本文主角登台亮相的时候了,G ...
- 大熊君大话NodeJS之------Http模块
一,开篇分析 首先“Http”这个概念大家应该比较熟悉了,它不是基于特定语言的,是一个通用的应用层协议,不同语言有不同的实现细节,但是万变不离其宗,思想是相同的, NodeJS作为一个宿主运行环境,以 ...
- 低版本IE浏览器 input元素出现叉叉的情况
都说是IE10之上的浏览器才有这个问题,恰巧我IE10之上都没有问题,反而是低版本的浏览器出现了这个问题.作为一个凭证,我先放一张图片在这里面. 之前无意中解决过这个问题,如今复现确实是没有解决,网上 ...
- 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。
********************************* 思路壹(也是网络上铺天盖地的通俗解决方案) 原因: 这是因为先安装了 .NET Framework , 随后启用了 .NET Fra ...
- android版微信5.2.1更新 支持微信聊天记录备份到电脑上
昨天微信 5.2.1 for Android 全新发布了,和微信 5.2.1 for iPhone一样,支持拍照分享,可以把照片发送给多个朋友,最重要的一个更新是支持微信聊天记录备份到电脑(可以通过腾 ...
- zabbix 3.0.4 Nginx 性能监控
搭建Nginx 安装pcre-devel .zlib-devel支持包 [root@test /]# yum -y install pcre-devel zlib-devel 创建nginx用户 [r ...
- Dijkstra(歪果仁的名字真是长。。。)
Dijkstra算法又称为单源最短路径,所谓单源是在一个有向图中,从一个顶点出发,求该顶点至所有可到达顶点的最短路径问题. 设G=(V,E)是一个有向图,V表示顶点,E表示边.它的每一条边 ...
- [BZOJ3991][SDOI2015]寻宝游戏
[BZOJ3991][SDOI2015]寻宝游戏 试题描述 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择 ...
- Win7去除桌面残影的方法
用户升级到Win7系统后使用正常,就是系统桌面会留有残影,怎么样也去不掉,影响用户的使用,那么要如何将这些残影去掉呢?可从计算机属性中进行相关配置. 解决方法 一.在计算机面板上,右键点击“计算机”, ...
- COGS 2437 暗之链锁 II 题解
[题意] 给出一个有n个点的无向图,其中有n-1条主要边且这些主要边构成一棵树,此外还有m条其他边,求斩断原图的一条主要边和k条其他边使得图不连通的方案数mod109+7的值. 注意,就算你切断一条主 ...