import org.lep.leetcode.groupanagrams.GroupAnagram;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/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
*
* 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.
*/
public class GrayCode { /**
* 将十进制(二进制)转换为格雷码
*
* 格雷码:wiki:https://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81
*
* 二进制转格雷码的方法:
* G:格雷码 B:二进制码
* G(N) = (B(n)/2) XOR B(n)
*
* @param n 格雷码的位数
* @return
*/
public Integer[] binaryToGrayCode (int n) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < Math.pow(2, n); i++) {
list.add(i >> 1 ^ i);
}
return list.toArray(new Integer[list.size()]);
} public static void main(String[] args) {
GrayCode grayCode = new GrayCode();
System.out.println(Arrays.toString(grayCode.binaryToGrayCode(0)));
System.out.println(Arrays.toString(grayCode.binaryToGrayCode(1)));
System.out.println(Arrays.toString(grayCode.binaryToGrayCode(2)));
System.out.println(Arrays.toString(grayCode.binaryToGrayCode(3)));
}
}

leetcode — gray-code的更多相关文章

  1. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. [leetcode]Gray Code @ Python

    原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...

  3. LeetCode——Gray Code

    Description: The gray code is a binary numeral system where two successive values differ in only one ...

  4. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  5. LeetCode: Gray Code [089]

    [题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  6. LeetCode: Gray Code 解题报告

    Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...

  7. [转载]LeetCode: Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  8. Leetcode Gray Code

    题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...

  9. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  10. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

随机推荐

  1. [转]XModem协议

    出处:XModem协议 XModem协议介绍:XModem是一种在串口通信中广泛使用的异步文件传输协议,分为XModem和1k-XModem协议两种,前者使用128字节的数据块,后者使用1024字节即 ...

  2. js方法实现--上传文件功能

    function createUploadForm(fileElementId, data, curFileList) { var id = new Date().getTime(); var for ...

  3. 微信公众平台测试号 “微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003”

    设置"网页授权获取用户基本信息" 点击"修改" 弹出"OAuth2.0网页授权",注意域名不加"https://"或&q ...

  4. hbase数据原理及基本架构

    第一:hbase介绍 hbase是一个构建在hdfs上的分布式列存储系统: hbase是apache hadoop生态系统中的重要一员,主要用于海量结构化数据存储 从逻辑上讲,hbase将数据按照表. ...

  5. JavaScript经典作用域问题(转载)

    题目 var a = 10; function test(){ a = 100; console.log(a); console.log(this.a); var a; console.log(a); ...

  6. 【高并发架构】Redis缓存高并发之-主从架构

    Redis主从架构 到目前为止,Redis Cluster 能实现很好的性能,但如果只是缓存几个G的数据,那么单机Redis就足够了,但缓存主要用来读的,单机的QPS有一定的极限,一两万QPS一台应该 ...

  7. 记一次JVM故障排除

    今天,自己开发的事件驱动的java大规模爬虫程序上线了几个新任务后突然异常. 异常: 程序业务异常,经查看CPU利用率满,内存满,一直报OOM,目测有内存泄露.如下图所示,四核16G的内粗,CPU高达 ...

  8. swust oj 1010

    折半查找的实现 1000(ms) 10000(kb) 2877 / 11213 编写程序实现折半查找算法. 输入 第一行是查找表的长度n 第二行是查找表中的数据元素 : 第三行是要查找的数据元素的关键 ...

  9. HTML文档结构

    下面对HTML文档结构进行一 一解释: 1.文档声明:既不是元素,也不是注释: 代码格式:<! DOCTYPE html> 注:必须写在HTML文档的第一行 原因:告诉浏览器使用哪个版本的 ...

  10. .NET Standard 2.0正式发布了

    亦可赛艇 前天(2017年8月14日),.NET Standard 2.0正式版终于发布了,与之相配套的.NET Core 2.0也同时正式发布,真是令人振奋. 详情请看:https://blogs. ...