89. Gray Code(公式题)
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.
公式:
G(i) = i^ (i/2).
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
for(int i =0;i<1<<n;i++)
res.add(i^i>>1);
return res;
}
}
89. Gray Code(公式题)的更多相关文章
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- 89. Gray Code返回位运算的所有生成值
[抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
随机推荐
- oracle扩展dblink数。
[标记]在进行数据迁移时:出现 Compilation errors for PROCEDURE ZDGAME.GFF_FETCH_MZR_LOG Error: ORA-04052: error oc ...
- sqlmap tutorial
svn checkout https://svn.sqlmap.org/sqlmap/trunk/sqlmap sqlmap-dev sqlmap.py -u "http://www.isl ...
- JS选中清空
var inputArray = document.getElementsByTagName("input"); var strArray = []; ; i < input ...
- Servlet MVC 项目实战实例
MVC的架构模式,一直是JavaEE开发中所遵循的标准,如今很多框架都已经很好的实现了MVC,像大家所熟知的Struts,SpringMVC,JSF等,但是如果没有任何框架的支持,仅仅通过JavaWe ...
- css - Position定位属性与层级关系
今天同事发现一个有意思的问题,关于position的层级关系的,他要不说我也没注意过 测试后果然有趣,有待深入研究: <!DOCTYPE html> <html> <he ...
- Unity3D笔记三 物理引擎
一.物理引擎 1.物理引擎就是模拟真实世界中物体碰撞.跌落等反应,通过Ballance.愤怒的小鸟来理解什么是物理引擎.Unity的物理引擎使用的是NviDIA的PhysX.2.选中一个游戏对象,主菜 ...
- WCF(五) 深入理解绑定
适用于本机WCF-WCF交互性能最佳的绑定: 允许跨主机,但只能用于部署同一台主机上,不能访问命名管道 netNamePipeBinding总结 一 WCF与SOA SOA是一种通过为所有软件提供服务 ...
- 修改js confirm alert 提示框文字
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 【node】------module.exports&&exports之间的区别------【巷子】
1.再讲module.exports 与exports之间的区别的时候我们先来回顾一下js里面的引用传递 001.引用传递 var arr = [10,20,30]; var newarr = arr ...
- loading图标modal弹窗 和jquery ajax的关系
在ajax配置中 ,async:false,非异步,modal窗口会失效,只有重新设置为async:true,或者删除async的设置,则loading的模态框才能展示出来 loading图标的模态框 ...