【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 ...
随机推荐
- 【CF1141G】Privatization of Roads in Treeland
题目大意:给定一个 N 个点的无根树,现给这个树进行染色.定义一个节点是坏点,若满足与该节点相连的至少两条边是相同的颜色,求至多有 k 个坏点的情况下最少需要几种颜色才能进行合法染色. 题解:考虑一个 ...
- linux free命令
Linux上的free命令详解 free命令的所有输出值都是从/proc/meminfo中读出的 total used free shared buffers cached Mem: -/+ buff ...
- 将本地html文件拖到IE8浏览器无法打开,直接弹出一个下载的对话框
查看一下注册表[HKEY_CLASSES_ROOT\.htm]和[HKEY_CLASSES_ROOT\.html]的ContentType值是否都为“text/html”
- How MVC pattern Flows
以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)
- Mysql DML DCL DDL
在介绍这些SQL语言之前,先罗列一下mysql的常用数据类型和数据类型修饰,供查询参考 后面的带数字表示此类型的字段长度 数值型: TINYINT 1 ,SMALLINT 2,MEDIUMINT 3 ...
- 关于react-native项目在MacBookPro环境下打包成IPA
苹果开发者打包是需要接入公司的开发者账户里面的.看是企业账户还是什么,具体我不太清楚. 不过打包的方法倒是大同小异. 我们一起新建项目,先跑起来这个项目 npm install -g yarn rea ...
- 为什么要用PolyFill(JS中的修补匠)
var users = [{name:"zhangsan",age:18},{name:"jack",age:20}]; 这是一个对象数组.如果我们要查询名字为 ...
- PMP项目管理的49个过程,一张图让你全部了解
项目管理的49个过程,看表格显得比较单调,印象也不是很深,所以今天小编就给大家发一张图片,可以用一张图就能生动又详细的了解PMP项目管理的49个过程. 大家看完是不是觉得一目了然了呢,图片上传后不 ...
- Linux批量修改(删除)文件名某些字符(rename命令)
假设在路径C:/下存在多个类似以下的文件名 file_nall_abc1.txt file_nall_abc2.txt file_nall_abc3.txt file_nall_abc4.txt fi ...
- postman charles设置代理
1.首先现在证书 2.确保手机和电脑是同一网络,ifconfig查看电脑ip 3.设置手机->配置代理-手动-服务器(电脑的ip)-端口8888 4.设置charles代理 5.重新启动char ...