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 ...
随机推荐
- JavaScript如何检查网站是可以访问
<html><head><title>JavaScript如何检查网站是可以访问</title> <script> //JavaScript ...
- git和github
GIT是一款分布式版本控制系统.与SVN相比可以不依赖网络,并且对分支和合并有更好的支持.但是命令稍微复杂一些,这里简单介绍使用git将项目上传到github. 首先GIT安装只需要去官网下载安装即可 ...
- 一个简单的零配置命令行HTTP服务器
http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...
- html5 绘制图片 drawImage
要在绘图上下文中绘制图片,可以使用 drawImage 方法.该方法有三种不同的参数: drawImage(image,dx,dy) drawImage(image,dx,dy,dw,dh) d ...
- windbg学习---!thread和.thread
!thread扩展显示目标系统中线程包括ETHREAD块在内的摘要信息.该命令只能在内核模式调试下使用 !thread [-p] [-t] [Address [Flags]] -p 显示拥有该线程的进 ...
- DIOCP之编写第一个应用程序(三)
Client 设计功能如下: 1.建立与服务器连接 2.请求连接时,加密密码,采用Base64编码 3.定时发送心跳告诉服务器在线(长连接,用于接收推送信息) 4.进行相关的数据处理与交互 第一步:创 ...
- eclipse maven spring mvc el表达式无效
http://www.myexception.cn/javascript/2031310.html
- fiddler 配置
fiddler 是一个抓包工具: 配置模拟器:(逍遥游安卓模拟器) 逍遥参考:http://www.xyaz.cn/thread-163-1-1.html 1.启动模拟器后,点击设置,点击进入Wi-F ...
- replaceWith() 和 replaceAll() 方法替换元素节点
$("#Span1").replaceWith("<span title='replaceWith'>陶国荣</span>"); $(& ...
- JavaScript学习(一)—处理事件
一.处理事件(一) 事件(event)是用户在访问页面时执行的操作.提交表单和在图像上移动鼠标就是两种事件.当浏览器探测到一个事件时,比如用鼠标单击或按键,它可以触发与这个事件相关联的JavaScri ...