Gray Code
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 - 2Note:
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.
以上是题目。想的时候要先把问题减少到最小规模,也就是1位。然后再扩展成两位,再扩展成3位,从而发现当位数增加时低位的变化规律。把问题简化到最小规模,且不改变问题本质的方法非常适用于解决实际问题。
这题的数字变化规律是: 每向高位增加一位,则所有低位的二进制按照从下到上的逆序复制一遍就行了。比如例子中的3,看成是最高位的1和3上一个数字对应3的低位部分的复制(复制的是1的位)。4就是最高位的1加上复制的0的位。写成代码如下:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(0);
if (n > 0)
res.push_back(1);
int cnt = 2;
for (int k = 1; k < n; k++)
{
int preBitsNum = std::pow(2, k);
/*if (preBitsNum == 1)
preBitsNum = 0;*/
for (int i = 0; i < preBitsNum; i++)
{
int cur = 1 << k;
cur |= res[cnt++ - i * 2 - 1];
res.push_back(cur);
}
}
return res;
}
Gray Code的更多相关文章
- [LeetCode] 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 ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- 企业级监控平台开发之nagios二次开发(七)
背景: A公司里有很多服务器(>3000台),每台服务器都有不同的用途,如DB Server.WEB Server.ESXI等,每个组使用其中的一批,每个组可能有多个服务器管理员.现在问题出来了 ...
- iOS开发中多线程间关于锁的使用
为什么需要使用锁,当然熟悉多线程的你,自然不会感到陌生. 那你在代码中是否很好的使用了锁的机制呢?你又知道几种实现锁的方法呢? main.m 1 int main(int argc, const ch ...
- 建筑材料系统 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发
框架介绍: 1.基于 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发 2.采用MVC的框架模式,具有耦合性低.重用性高.生命周期成本低.可维护性 ...
- mvc Razor 视图中找不到 ViewBag的定义
在Razor 视图中,我们有时会看到 ViewBag.Title 下会划一个红线,当鼠标放上去的时候会提示这样的一个错误: 找不到编译动态表达式所需的一种或多种类型,是否缺少引用? 但在项目启动运行时 ...
- 查看已安装的CentOS版本信息:
如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...
- 解决 Gnome3 窗口背景是黑色的问题
. . . . . Gnome3 在 Ubuntu 上窗口背景颜色经常是黑色的,终于找到了解决办法,其实很简单: >$ gsettings set com.canonical.desktop.i ...
- CURL详解(转载)
curl_setop()函数中的参数中文说明 curl_setop()函数中的参数中文说明 curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,value是这个 ...
- ArcMap 操作笔记
1.SQL查询(in) select * from table where PointID in ('1','2')
- 【&】位与运算符【|】位或运算符之权限控制算法
[&]位与运算符: 按位与运算符"&"是双目运算符. 其功能是参与运算的两数各对应的二进位相与.只有对应的两个二进位均为1时,结果位才为1 ,否则为0.参与运算的数 ...
- 第十二章 redis-cluster搭建(redis-3.2.5)
redis集群技术 redis2.x使用客户端分片技术 redis3.x使用cluster集群技术 一.环境 os:centos7 ip:10.211.55.4 redis:3.2.5 gem-red ...