LeetCode OJ: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
看着好像很难的样子,但是实际上就是一个二进制码到格林吗的转换而已,代码如下(这个本科的时候好像学过,但是不记得了,看了下别人的):
class Solution {
public:
vector<int> grayCode(int n) {
int total = << n;
vector<int> ret;
for(int i = ; i < total; ++i){
ret.push_back(i>>^i);
}
return ret;
}
};
java版本的代码如下所示:
public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> ret = new ArrayList<Integer>();
int num = 1 << n;
for(int i =0 ; i< num; ++i){
ret.add(i>>1^i);
}
return ret;
}
}
还有一种是总结规律的写法:
public String[] GrayCode(int n) { // produce 2^n grade codes
String[] graycode = new String[(int) Math.pow(, n)]; if (n == ) {
graycode[] = "";
graycode[] = "";
return graycode;
} String[] last = GrayCode(n - ); for (int i = ; i < last.length; i++) {
graycode[i] = "" + last[i];
graycode[graycode.length - - i] = "" + last[i];
} return graycode;
}
LeetCode OJ:Gray Code(格林码)的更多相关文章
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] 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 (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- Java for LeetCode 089 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
随机推荐
- c++之旅:函数模板
函数模板 函数模板主要是泛型在函数的中的应用,通过泛型可以让函数处理各种各样的数据类型 简单的列子 #include <iostream> using namespace std; tem ...
- jquery ajax修改全局变量或者局部变量示例代码
今天在工作的时候遇见一个问题,利用ajax到action中查询返回的值付给全局变量或者局部变量,总是改变不了,后来查找资料才发现需要添加async:false 示例代码: var status=1; ...
- SQL条件!=null查不出数据
今天有一条sql需要某两个字段不能为空,当然是不能为null也不能为空字符串啦. 然后就开始写 WHERE ( order_amount != null and order_amount != '' ...
- 20145327 《Java程序设计》第三周学习总结
20145327 <Java程序设计>第三周学习总结 教材学习内容总结 对象:存在的具体实体,具有明确的状态和行为. 类:具有相同属性和行为的一组对象的集合,用于组合各个对象所共有操作和属 ...
- 20145328 《Java程序设计》实验一实验报告
20145328 <Java程序设计>实验一实验报告 实验名称 Java开发环境的熟悉(Windows + IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA 编 ...
- 20135320赵瀚青LINUX内核分析第一周学习笔记
赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.概述 第一周的学习内容主 ...
- # PHP学习笔记之一
PHP学习笔记之一 标签(空格分隔): PHP 资料来源:慕课网PHP入门篇.PHP学习手册 一.变量 变量定义 $变量名 = 变量值; $var = "xxx"; 变量类型查看 ...
- Win7旗舰版中的IIS配置asp.net 完美通过版,附代码 以及出现的 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d
先解决问题:“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d 图: 其他的解决方案 ...
- 6.scala中的包
版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=287 排版乱?请移步原文获得更好的阅读体验 1.基础特性 scala中的包和java中的包类似,都是用来在大型工 ...
- Python 实现C语言 while(scanf("%d%d", &a, &b) != EOF) 语句功能
reference:Python 实现C语言 while(scanf("%d%d", &a, &b) != EOF) 语句功能 在python中,无法通过input ...