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 ...
随机推荐
- jstack用法
第一步先找出Java进程ID,我部署在服务器上的Java应用名称为mrf-center: root@ubuntu:/# ps -ef | grep mrf-center | grep -v grep ...
- 日志处理(三) logback 手动加载(转)
本文转自:http://www.2cto.com/kf/201302/191149.html 一共两个java文件,第一个是例子,第二个是配置文件加载类; LogbackTest.java /* * ...
- 详解Java中的clone方法 -- 原型模式
转自: http://blog.csdn.net/zhangjg_blog/article/details/18369201 Java中对象的创建 clone顾名思义就是复制, 在Java语言中, ...
- CSS清除浮动大全的8种方法
清除浮动是每一个 web前台设计师必须掌握的机能.css清除浮动大全,共8种方法. 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的 ...
- Windows下编译live555源码
Windos下编译live555源码 环境 Win7 64位 + VS2012 步骤 1)源码下载并解压 在官网上下载最新live555源码,并对其进行解压. 2)VS下建立工程项目 新建Win32项 ...
- Openwrt架设GIT服务
#下载宝刷LEDE版系统后, 在上面安装git包 opkg update opkg install git #安装好后在将git仓库装到SD(TF)卡上 #用fdisk对SD 卡分区 #fdisk / ...
- 在父页面访问iframe的东西
playBox是iframe里的元素id
- 20145310 《Java程序设计》第2周学习总结
20145310 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要学习了java的基础语法. java中的基本类型主要可区分为,整数.字节.浮点数.字符与布尔.整数shor ...
- 20145328 《Java程序设计》第0周学习总结
20145328 <Java程序设计>第0周学习总结 阅读心得 从总体上来说,这几篇文章都是围绕着软件工程专业的一些现象来进行描述的,但深入了解之后就可以发现,无论是软件工程专业还是我们现 ...
- 20145333茹翔 《Java程序设计》实验四 实验报告
实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等).解决办法(空洞的方法如"查网络&q ...