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. Exp1 PC平台逆向破解 20164302 王一帆

    1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程 ...

  2. [HEOI/TJOI2016]序列

    Description: 给你一个序列,每个数可能变化为另一个数,每次最多有一个数变化 求最长的子序列,无论如何变化,这个子序列都不下降 Hint: \(n \le 10^5\) Solution: ...

  3. 如何修改SnipeIT的部分设置

    作为一款开源的资产管理系统,Snipe-IT非常的好用又结实,但是原始设置对中国用户有些不方便,部分汉化没有完成,需要直接修改代码,下面把常用的修改记录如下: 1.修改资产打印标签中的文本名称 找到  ...

  4. VMware Workstation安装Red hat7.0联网问题总结

    1.在red hat7当中iconfig命令是被取消了的,开发者用ip addr命令取代了ifconfig命令. 当然也是可以用ifconfig命令的 (前提是你安装的linux是可以联网的): 1) ...

  5. pycharm下虚拟环境建立,django项目建立等情况说明

  6. 微信小程序之canvas绘制海报分享到朋友圈

    绘制canvas内容 首先,需要写一个canvas标签,给canvas-id命名为shareBox <canvas canvas-id="shareBox"></ ...

  7. window7环境下ZooKeeper的安装及运行

    简介 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  8. 使用Arrays.asList抛出java.lang.UnsupportedOperationException

    使用 Arrays.asList("str1", "str2")生成的List,不能进行remove.add操作,会产生异常java.lang.Unsuppor ...

  9. 【尺取法】Jurisdiction Disenchantment

    [尺取法]Jurisdiction Disenchantment PROBLEM 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The Super League of Paragons ...

  10. ubuntu18.04新体验

    虽然ubuntu18.04LST版本早出来了,但自己原来的ubuntu16.04还可以用,就懒得折腾了. 但最近ubuntu崩了,就想尝尝鲜...结果发现还挺好用的,准确地说,ubuntu是越来越好用 ...