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. Spark环境搭建(五)-----------Spark生态圈概述与Hadoop对比

    Spark:快速的通用的分布式计算框架 概述和特点: 1) Speed,(开发和执行)速度快.基于内存的计算:DAG(有向无环图)的计算引擎:基于线程模型: 2)Easy of use,易用 . 多语 ...

  2. centos7系统下搭建docker本地镜像仓库

    ## 准备工作 用到的工具, Xshell5, Xftp5, docker.io/registry:latest镜像 关于docker的安装和设置加速, 请参考这篇博文centos7系统下 docke ...

  3. java 新手指南

    Java新手指南 不小心走上了一条不归路的我 因为对可视化感兴趣,然后学了MFC,发现MFC好麻烦啊,不如开发APP吧,刚学开发APP,艹,居然是用java做开发,那只好学java了,,呜呜,不知道什 ...

  4. c# 集合的长度为什么是可变的

    摘要: 写在前面:此随笔仅仅是作为个人学习总结,有不对的地方,请各位前辈指正O(∩_∩)O........ 一: 引入 在学习集合之前我们都学习过数组.可以知道数组的长度在声明的时候就已经被固定了,不 ...

  5. nvidia-smi GPU异常消失 程序中断

    GPU型号为NVIDIA的1080Ti,最近出现的状况的是某一个GPU突然就出问题了,如果在该GPU上有运行程序的话则程序中断,nvidia-smi显示出来的GPU则少了这一个. 1.一开始怀疑是温度 ...

  6. 【CSS 第五天】背景,边框

    总结一下今天所学习的内容,如下: 背景 属性 例子或作用 background background: #00FF00 url(bgimage.gif) no-repeat fixed top; ba ...

  7. unittest测试套件

    测试套件就是测试集,测试集是测试用例的集合. a.按用例顺序执行(addtest) 当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行. import unittest ...

  8. H5混合开发问题总结

    1.This application is modifying the autolayout engine from a background thread, which can lead to en ...

  9. Mesos源码分析(15): Test Executor的运行

    Test Executor的代码在src/examples/test_executor.cpp中   int main(int argc, char** argv) {   TestExecutor ...

  10. .NET Core跨平台的奥秘[下篇]:全新的布局

    从本质上讲,按照CLI规范设计的.NET从其出生的那一刻就具有跨平台的基因,这与Java别无二致.由于采用了统一的中间语言,微软只需要针对不同的平台设计不同的虚拟机(运行时)就能弥合不同操作系统与处理 ...