[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 ...
随机推荐
- json ubuntu下安装
1.首先安装scons scons是linux下的自动构建工具,类似cmake. 下载地址wget http://prdownloads.sourceforge.net/scons/scons-2.2 ...
- Git工具和GitHub的使用
一.Git工具的安装 1)centos系统下安装 1.1)查看环境 [root@gitlab ~]# rpm -qa centos-release centos-release--4.1708.el7 ...
- eclipse安装提要
svn 插件安装http://subclipse.tigris.org/update_1.12.x教程地址http://jingyan.baidu.com/article/f71d60376b4c57 ...
- day10作业—(闭包迭代器递归)
补充:一个星号的 打散和聚合 a, b , *c = [1,2, 1,4] print( a , b , *c) #1 2 1 4 print(a, b, c) #1 2 [1, 4] *c , = ...
- kbmmw 与extjs 的初次结合
前面写了extjs 的安装,今天写一下kbmmw 与extjs 的结合,参照delphi 产品经理marco文章 . 由于extjs 设计时要读取服务器端的数据,所以先要做一个rest 服务器. 先要 ...
- Java的GUI如何能够切换界面
在设计GUI的时候,会遇到类似于菜单栏的切换,如何做到界面切换 使用一个JTabbedPane组件,就可以实现界面的切换问题. 在使用的时候可以将里面要使用的组件进行一个封装,封装成一个Panel.再 ...
- shell入门练习
**定义局部变量, 局部变量在退出Shell客户端时会失效** **单引号:原样输出** **双引号:如果里面有变量,会输出变量** **没有引号:输出变量** 可以在调用脚本的时候给脚本传递参数,脚 ...
- ODT(old driver tree)详解(带例题)
文章目录 ODT简介 实现前提&&实现原理 初始化 split操作 assign操作 其它操作 区间第k小 区间加 区间所有数的k次方和 几道水题 ODT简介 ODT(old driv ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)
https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...