【LeetCode】89.Gary Code
Problem:
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:
-
-
-
-
Solution:
理解格雷码和二进制的转换关系这题就很简单了。
格雷码的特点在于相邻两个数字仅有一位不同。题中给出的示例是【00,01,11,10】,原题提示中也说到格雷码的顺序可能不止一种,如【00,10,11,01】亦可,只需满足相邻两数只有一位不同这一条件。
按题示序列规则,对二进制数进行右移运算和异或运算可以得出相应格雷码,如:
// i ^(i>>1)
()^→()
()^→()
()^→()
()^→()
AC Code(C++):
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> seq;
for(int i=;i<(<<n);i++){
seq.push_back(i^(i>>));
}
return seq;
}
};
【LeetCode】89.Gary Code的更多相关文章
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【LeetCode】89. Gray Code (2 solutions)
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- day27
27.01 反射(类的加载概述和加载时机) 1.类的加载 当程序要使用某个类时,如果该类还未加载到内存中,系统会通知加载,连接,初始化三步来实现对这个类初始化 a.加载 是指将.class文件读入内存 ...
- [HAOI2008]圆上的整点(数论)
题目的所求可以转化为: \(y^2=r^2-x^2\)(其中r,x,y均为整数) 即\(y^2=(r-x)(r+x)\)(其中\(r,x,y\)均为整数) 不妨设\((r-x)=d*u\)------ ...
- CF341E Candies Game
题目链接 题意 有\(n\)个盒子,第\(i\)个盒子里面有\(a_i\)个糖果.每次选择两个盒子\(i,j\),假设\(a_i \le a_j\).然后从第\(j\)个盒子中拿出\(a_i\)个糖果 ...
- hdu4899 Hero meet devil
题目链接 题意 给出一个长度字符串\(T\),其中只包含四种字符\((A,C,G,T)\),需要找一个字符串\(S\),使得\(S\)的长度为\(m\),问\(S\)和\(T\)的\(lcs\)为\( ...
- (转)C++ 值传递、指针传递、引用传递详解
一直以来对函数的值传递引用传递理解很模糊,这篇文章可以说是给自己扫盲了. 值传递:实参不会发生改变,是因为形参传递的是不是实参的源地址(形参和实参地址不一样).不影响实参 指针传递:本质也是值传递,只 ...
- python学习笔记—Day1
1. python使用<变量名>=<表达式>的方式对变量进行赋值 a=1; python中数分为整数和浮点数 字符串的定义一定要用引号,单引号和双引号是等价的 三引号用来输入包 ...
- msvcp100d.dll文件丢失,解决找不到msvcp100d.dll的问题
http://www.jb51.net/dll/msvcp100d.dll.html msvcp100d.dll控件常规安装方法(仅供参考): 一.如果在运行某软件或编译程序时提示缺少.找不到msvc ...
- java和c#值类型和引用类型
java数据类型分为基本数据类型和引用类型 基本数据类型:int float double bool char byte 引用数据类型:string array class interfa ...
- discuz 3.1论坛快照被百度劫持解决方案
最近很郁闷,遇到一个很棘手的问题.我们公司有个论坛在百度查看快照信息的时候全部都是博彩信息,但是打开却无博彩信息显示.在快照中查看是这样的 百度快照查看图: 经过思考,怀疑是网站中有网页被改动了,在某 ...
- 如何修改const常量值
总结:这个跟计算机语言类别和编译器有关,本文是在linux环境下说明的. 分两种情况: 1. C语言: 2. C++语言: /*! * \Description: * \author scictor ...