[leetcode.com]算法题目 - 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.
根据题目中的意思,我们只要求出一组合适的解即可。正确答案非常精妙(我没有想出来。。。),充分利用位运算。看来对于位运算的知识我还是要再加强。
答案出处:http://www.2cto.com/kf/201305/215316.html
class Solution {
public:
vector<int> grayCode(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result;
int m=<<n;
for(int i=;i<m;i++)
{
result.push_back(i^(i>>));
}
return result;
}
};
参考答案
思路:首先,1<<n 是用来计算2的n次方。i^(i>>1)是格雷码的计算方法,表示第i个数是几。好久不用都想不起来了。。。
[leetcode.com]算法题目 - Gray Code的更多相关文章
- LeetCode(87) Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- LeetCode题目:Gray Code
原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
随机推荐
- *args 和**kwargs 的溯源
*args:arguments:表示参数,代表一个tuple**kwargs:表示关键字参数,代表一个dict 也就是keyword args.keyword就表示字典,也就是关键字.为什么叫关键字. ...
- Sophus链接错误
错误指示如下: CMakeFiles/run_vo.dir/run_vo.cpp.o: In function `main': run_vo.cpp:(.text.startup+0x1086): u ...
- 【Tomcat】Tomcat + Memcached 实现session共享
概述 web项目中,Tomcat的访问量总是有限的,这时候就需要用到Tomcat集群,多个Tomcat的时候就要考虑Session共享的问题,这里介绍一种使用Memcached做Session共享的解 ...
- [js]获取网页屏幕可见区域高度
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...
- CRC在线计算工具
http://www.lammertbies.nl/comm/info/crc-calculation.html
- Java的GUI如何能够切换界面
在设计GUI的时候,会遇到类似于菜单栏的切换,如何做到界面切换 使用一个JTabbedPane组件,就可以实现界面的切换问题. 在使用的时候可以将里面要使用的组件进行一个封装,封装成一个Panel.再 ...
- oracle 替换字符 replace
替换字符: select replace('xxyyzz','zz','') from dual >> xxyy 同时替换多个字符: select replace(replace('x ...
- 2018.11.04 洛谷P2679 子串(线性dp)
传送门 为什么前几年的noipnoipnoip总是出这种送分题啊? 这个直接线性dpdpdp不就完了吗? f[i][j][k][0/1]f[i][j][k][0/1]f[i][j][k][0/1]表示 ...
- 2018.10.30 NOIP模拟 排列树(树形dp+组合数学)
传送门 考试的时候乱搞过了. 其实题目就是让你求拓扑排序方案数. 直接树形dpdpdp然后组合数转移一下就行了. 乱搞代码
- hdu-1042(大数+万进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 参考文章:https://blog.csdn.net/tigerisland45/article ...