题目: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
这道题其实就是产生格雷码(简直废话~),格雷码是有一定规律的,数电课上学过一点格雷码的规律,但是对于编程解决问题没啥用啊- -,我们还是来找规律吧。4位的,我手写最多写4位000000010011011001110101010011001101111111101010101110011000按列来看,从左往右数第0列(从0列开始),前半部分为0,后半部分为1。第1列,等分成上下两部分,第一部分的前半部分为0,后半部分为1。第二部分的前半部分为1,后半部分为0。后面继续看,可以看出规律了。把第i列分成2^i部分,奇数部分的前半部分为0,后半部分为1,偶数部分的前半部分为1,后半部分为0。或者用另外一种表述方法,我们可以把本次的划分看作前一次的划分的一个部分,如果是上次划分的前半部分,则符合前面的奇数部分。后半部分则符合前面的后半部分。代码如下:
 1 /*
 2 使用分治法,如果本次属于前一次二分的前半部分,则本次的前半部分第n位填0,后半部分第n位填1
 3             如果本次属于前一次二分的后半部分,则本次的前半部分第n位填1,后半部分第n位填0
 4 第一次填写,设为前半部分。代码中left代表前半部分-_-。
 5 */
 6
 7 void setGray(int *num, int start, int end, bool left, int n)
 8 {
 9     int lbit, rbit, i = 0;
10     int mid = (start + end) >> 1;
11
12     if (left)
13     {
14         lbit = 0;
15         rbit = 1 << n;
16     }
17     else
18     {
19         lbit = 1 << n;
20         rbit = 0;
21     }
22
23     for (i = start; i <= mid; ++i)
24         num[i] = num[i] | lbit;
25
26     for (i = mid + 1; i <= end; ++i)
27         num[i] = num[i] | rbit;
28
29     if (n > 0)
30     {
31         setGray(num, start, mid, true, n - 1);
32         setGray(num, mid + 1, end, false, n - 1);
33     }
34 }
35
36
37 int *grayCode(int n, int *returnSize)
38 {
39     *returnSize = 1 << n;
40     int *ans = (int *)malloc(*returnSize * sizeof(int));
41     int i = 0;
42     for (i = 0; i < *returnSize; ++i)
43     {
44         ans[i] = 0;
45     }
46     if (n > 0) setGray(ans, 0, *returnSize - 1, true, n - 1);
47     return ans;
48 }

转载于:https://www.cnblogs.com/DennisXie/p/4792928.html

Gray Code LeetCode 89相关推荐

  1. 89. Gray Code - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 1 ...

  2. 【格雷码】LeetCode 89. Gray Code

    LeetCode 89. Gray Code Solution1:我的答案 穷举法,比较笨 注意:牢记vector中find()函数的用法 class Solution { public:vector ...

  3. [LeetCode]89.Gray Code

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

  4. LeetCode(89):格雷编码 Gray Code(Java)

    2019.7.19 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 智力题,本题的关键在于搞清楚格雷编码的生成过程, G(i) = i ^ (i/2). 如 n = 3: G( ...

  5. LeetCode Gray Code(回溯法)

    问题:gray code是一种二进制数字系统,两个连续的数只有一位不同.给出一个正整数n,n表示gray code的位数,输出gray code. 思路:使用回溯法.通过枚举0,1,2,3的gray ...

  6. [LeetCode]Gray Code

    题目描述:(链接) The gray code is a binary numeral system where two successive values differ in only one bi ...

  7. leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

    2019独角兽企业重金招聘Python工程师标准>>> 一.题目描述 The gray code is a binary numeral system where two succe ...

  8. Gray Code(格雷码) C++多方法实现

    简介 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即"首尾相连",因此又称循环 ...

  9. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 second Me ...

最新文章

  1. springmvc二十三:日期格式化
  2. Service 和 doGet 和 doPost 方法的区别
  3. servlet和jsp中间的交互
  4. java jsp学习指南_JSP教程–最终指南
  5. MySQL水平分区代理Spock Proxy(一)
  6. sublime text 3 插件推荐?
  7. 音视频开发(14)---智能视频解决方案
  8. k8s 拉取镜像失败_k8s 拉取私有仓库失败
  9. 概念模型——分析模式学习笔记
  10. 图像的三种分形维数的计算方法
  11. lync 2013标准版安装
  12. C++图书ISBN码校验
  13. 中兴机顶盒网关服务器地址,中兴全球首发高清双向网关型DVB机顶盒
  14. linux编辑搜索命令,Linux 命令大全提供 500 多个 Linux 命令搜索
  15. html5打开抖音链接,抖音主页链接在哪里弄(主页链接设置教程)
  16. mysql计算订单总金额_【写SQL语句】按照用户统计对应订单数和订单总金额?
  17. 软件企业必备的认证资质证书
  18. 可控硅的两种触发方式:移相触发和过零触发
  19. 【大会信息分享】新一代推荐算法核心技术与实践
  20. 确定性网络:从“尽力而为”到“确定承诺”

热门文章

  1. 修改Static控件的字体颜色
  2. springboot使用redisTemplate 报错:APP FAILED TO START Field template in required a single bean redis工具类
  3. Android 使用SWIG生成Jni代码
  4. Pixhawk的传感器数据(陀螺、加计等)流程
  5. HDU - 1027 全排列
  6. java 获取mp4 缩略图_java获取视频缩略图
  7. Tensorflow1.x 和 2.x如何读取ckpt中保存了那些参数
  8. mysql bit类型 使用select查询无法看到其值
  9. Leetcode题库 145.二叉树的后序遍历(递归 C实现)
  10. Java hibernate假外键_java – Hibernate:没有实体类的外键,只能通过id