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 ...
随机推荐
- Android4.0 Surface机制分析
1. java层面的Surface 对于Surface我们的认识主要是android的类Surface, android的文档描述Surface是"Handle onto a raw ...
- JavaScript常用算法
一.排序算法 1.Array.sort(function)(JavaScript原生排序算法)参数:比较函数(可选)若无参数,则按照首字母的ASCII码排序,比较函数的作用为确定排序 function ...
- JavaScript浮点运算,小数点精度
math.js JavaScript浮点运算,小数点精度 // JavaScript Document //数学函数 // 浮点数加法运算 function floatAdd(arg1, arg2) ...
- 《Java程序设计》实验3
20145318 <Java程序设计>实验3 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 PSP 队友链接 http://www.cnblogs.com/ ...
- 20145327 《Java程序设计》第五周学习总结
20145327 <Java程序设计>第五周学习总结 教材学习内容总结 try...catch:与C语言中程序流程和错误处理混在一起不同,Java中把正常流程放try块中,错误(异常)处理 ...
- Apache-solr
1.1. 下载 从Solr官方网站(http://lucene.apache.org/solr/ )下载Solr4.10.3,根据Solr的运行环境,Linux下需要下载lucene-4.10.3.t ...
- ArchLinux基本系统到XFCE4桌面搭建
Keep It Simple, Stupid 这是ArchLinux的哲学,更是一种人生哲学 好久没用linux了,这段时间因为一点点"破坏性"需求重新拾起linux用了一把 ...
- [Pytorch]PyTorch Dataloader自定义数据读取
整理一下看到的自定义数据读取的方法,较好的有一下三篇文章, 其实自定义的方法就是把现有数据集的train和test分别用 含有图像路径与label的list返回就好了,所以需要根据数据集随机应变. 所 ...
- css 固定宽度,自动换行
max-width: 200px; display: block; word-break: break-all:
- SpringBoot 玩转读写分离
环境概览 前言介绍 Sharding-JDBC是当当网的一个开源项目,只需引入jar即可轻松实现读写分离与分库分表.与MyCat不同的是,Sharding-JDBC致力于提供轻量级的服务框架,无需额外 ...