[LeetCode 题解]: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.
考察位操作:
从题目中例子可以发现这么一个规律:
假设结果数组为B,原始数组为A,则有: B[i] = A[i] ^ (A[i]>>). 例如 : A[] =
B[] = ^(>>)
= ^
= =
解法如下:
class Solution {
public:
vector<int> grayCode(int n) {
int len = pow(2.0,n),i,a;
vector<int> vi;
for(i=;i<len;i++)
{
a= i^(i>>);
vi.push_back(a);
}
return vi;
}
};
[LeetCode 题解]:Gray Code的更多相关文章
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] 89. 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 ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- Java for LeetCode 089 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LeetCode题目:Gray Code
原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
随机推荐
- 使用JAVA爬取去哪儿网入住信息
昨天帮一个商科同学爬取去哪儿网站的所有广州如家快捷酒店的所有入住信息. 就是上面的商务出行 xxx年入住这些东西 然而去哪儿的前端很强,在获取所有如家快捷酒店的时候就遇到了问题. 他显示的酒店列表是j ...
- leetcode341
数据结构设计类题目,参考网上的代码: /** * // This is the interface that allows for creating nested lists. * // You sh ...
- xmlhttp的OnReadyStateChange事件
这两天抽空升级了一下自己做的pon资料表,增加了OLT拓扑自动发现以及业务从MSE至OLT的全自动下发 等功能,让人没想到的是在处理xmlhttp回调时死活接收不到反馈信息, 在Excel论坛和微软官 ...
- Promethus安装指南
由于Prometheus是go语言写的,所以不需要编译,安装的过程非常简单,仅需要解压然后运行.Prometheus官方下载地址:https://prometheus.io/download/ 安装P ...
- java 蓝桥杯算法提高 _3K好数
nums[i][j] 存的是i位数的时候,首位数字是j的K好数的数目,i从1位开始的结果,去算2位时的结果,去算3位时的结果...最后得到l位的结果.K进制只是一个范围. import java.ut ...
- out.write(b,0,len)怎么解释?
参数 缓冲区——要写的数据 从——开始偏移量数据 len——写的字节数 返回值 这个方法不返回一个值. 异常 IOException
- Ztree右键事件,如何让指定的子节点不显示右键菜单。
这里我记录一下我自己的解决方案: 1.首先在Ztree的setting设置中加一个鼠标右键回调函数onRightClick,然后在加一个beforeRightClick(具体含义可以看官方API) v ...
- 要生成在[min,max]之间的随机整数,
import java.util.Random; public class RandomTest { public static void main(String[] args) { int max= ...
- Android有趣的全透明效果--Activity及Dialog的全透明(附android系统自带图标大全)[转]
原文地址:http://blog.csdn.net/sodino/article/details/5822147 1.Activity全透明 同学zzm给了这个有趣的代码,现在公布出来. 先在res/ ...
- [c++] polymorphism without virtual function
polymorphism without virtual function