Leetcode88: Gray Code
来源:互联网 发布:linux下怎么看java目录 编辑:程序博客网 时间:2024/06/01 10:04
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 - 001 - 111 - 310 - 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.
数学解:
从第0个开始,第i个gray code为:(i>>1)^i
class Solution {public: vector<int> grayCode(int n) { vector<int> res; int num = 1<<n; int i = 0; while(i<num) { res.push_back((i>>1)^i); i++; } return res; }};
0 0
- Leetcode88: Gray Code
- Gray Code
- Gray Code
- gray code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- Gray Code
- svm中的数学和算法
- whatsapp显示不全联系人怎么办
- 每天一个linux命令(25):linux文件属性详解
- ZOJ3432 Find the Lost Sock 字符串异或
- Html 图像
- Leetcode88: Gray Code
- 转载,写的不错的一个关于dshow的文章
- angularjs基础—$scope.$apply的应用
- C# 调用CMD
- 基于Arm板linux嵌入式系统RS485串口读写通讯
- linux中用命令mail发送邮件
- Android 自定义控件实现ListView索引
- 如何从国内镜像获取android源代码
- C# 获取文件名及扩展名