leetcode[88] Gray Code
题目:格雷码。
格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同。
现在给定一个数字n,然后给出格雷码所对应的数字。例如:
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
左边一列是格雷码,右边一列是对应的需要输出的十进制。
我们来观察一下格雷码的规律。如果是1那么是
0
1
如果是2的时候,其实就是在0和1前面加0,再在1,0前面加1。
3的时候就是把2的格雷码先加零,然后倒序前面加1,那么我们就可以按照这个规律
000
001
011
010
110
111
101
100
知道怎么来的,而且如果记录的话就是从0开始,往后记,而且因为前面加零的大小是不变的,那么先加入一个0后,之后每次记录在前面加1的即可。那么可以如下:
class Solution {
public:
vector<int> grayCode(int n)
{
vector<int> ans;
ans.push_back();
for (int i = ; i < n; ++i)
{
int left = <<i;
int len = ans.size();
for (int j = len-; j >= ; --j)
ans.push_back(left + ans[j]);
}
return ans;
}
};
leetcode[88] Gray Code的更多相关文章
- [LeetCode] 89. 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 ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Java for LeetCode 089 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LeetCode题目:Gray Code
原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- hdu 1316 How Many Fibs? (模拟高精度)
题目大意: 问[s,e]之间有多少个 斐波那契数. 思路分析: 直接模拟高精度字符串的加法和大小的比較. 注意wa点再 s 能够从 0 開始 那么要在推断输入结束的时候注意一下. #include & ...
- Rabbitmq 加入用户訪控制台(guest无法登陆控制台问题)
对于rabbitmq的guest用户无法訪问控制台的问题,是由于rabbitmq做了安全措施,禁止guest登陆控制台.须要我们自己创建用户进行登陆 1,运行加入用户命令 rabbitmqctl ad ...
- Install Typical IIS Workloads
原文 Install Typical IIS Workloads Introduction The IIS 7.0 and above modular architecture is designed ...
- 浅谈Hybrid技术的设计与实现(转)
前言 随着移动浪潮的兴起,各种APP层出不穷,极速的业务扩展提升了团队对开发效率的要求,这个时候使用IOS&Andriod开发一个APP似乎成本有点过高了,而H5的低成本.高效率.跨平台等特性 ...
- java 突击队注意事项:在路上
情绪: 灵活:让标准成为价格值.为了给你一个想法和标准,你可以有一个不同的使用.不是死扣定理.决这个问题. 看书:分两类,一类依据知识点进行罗列.并且结构清晰,能够看完一章有选择进行总结(不是笔记,总 ...
- boost在自己主动确定数据类型(BOOST_TYPEOF和BOOST_AUTO)使用
#include<boost/typeof/typeof.hpp> #include<vector> #include<iostream> #include BOO ...
- IOS 警告 收集
Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte t ...
- RTP 记录 log 该机制
我们 RCV 在这里,经常跑concurrent request RTP: Receiving Transaction Processor, 它主要是用来处理 RCV_TRANSACTIONS_INT ...
- 阅读INI档 - Delphi一片
程序往往需要读一些用户设置值.如何完成这一过程? B/S程序一般使用XML档.和C/S程序使用INI档. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读 ...
- POJ 2109 :Power of Cryptography
Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18258 Accepted: ...