代码nullgray code
最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--代码null
标题:
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.
代码如下:
vector<int> grayCode(int n) {
vector<int> result;
int m=1<<n;
for(int i=0;i<m;i++)
{
result.push_back(i^(i>>1));
}
return result;
}
文章结束给大家分享下程序员的一些笑话语录:
Borland说我很有前途,Sun笑了;Sun说我很有钱,IBM笑了;IBM说我很专业,Sybase笑了;Sybase说我数据库很牛,Oracle笑了;Oracle说我是开放的,Linux笑了;Linux说我要打败Unix,微软笑了;微软说我的系统很稳定,我们都笑了。
---------------------------------
原创文章 By
代码和null
---------------------------------