leetcode 题解: Gray Code
第一眼看到就是枚举,回溯法。
n位的ans就是在n-1的ans的基础上,每一个在首位加上1。
但是有个难点,要保证相邻两数之间只有一位在变化,怎么办?
首先
00
00 01
00 01 11 10
本来是傻乎乎的,直接加的,没有保证只变化1位。
后来发现,从00到01是只变了1位,那么我们给01加上2呢?就变成11, 给00加上2就变成 10。
每次逆序加2^n-1,这样就保证了相邻只变一位。
说不清,基本是蒙对的。碰上了。上代码吧。
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> ans;
ans.push_back();
for(int i=;i<n;i++)
{
int len = ans.size();
for(int j = len-; j >=; j--)
{
ans.push_back(ans[j] + pow(,i));
}
}
return ans;
}
};
leetcode 题解: 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] 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[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- 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]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
随机推荐
- html标签SEO规范
原文地址:http://blog.sina.com.cn/s/blog_6c3898dd0100whr7.html 1.<!--页面注解--> 2.<html> 3.<h ...
- tesseract 训练
下载chi_sim.traindata字库下载tesseract-ocr-setup-3.02.02.exe 下载地址:http://code.google.com/p/tesseract-ocr/d ...
- 猴哥来了-游戏开发记录17-微信排行榜bug
上线后排行榜bug 1.排序算法 const dataSorter = (gameDatas, field = Consts.OpenDataKeys.LevelKey) => { let d ...
- 封装MemoryCache
一.定义一个缓存接口IChace using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- 让WordPress支持google AMP
1.关于AMP 在移动互联网的时代,尽管网站响应式设计可以满足多屏(pc.手机.ipad等)浏览,但google在2015年10月推出了更快移动页面访问速度的技术-Accelerated Mobile ...
- Struts2 --简单留言系统
1.创建数据表,与servlet中相同 2.创建web项目,添加struts2模块,url 选 /*,web.xml中会自动注册struts2,同时src下会自动生成struts2配置文件struts ...
- 分布式系统的Raft算法
好东西~~ 英文动画演示Raft 过去, Paxos一直是分布式协议的标准,但是Paxos难于理解,更难以实现,Google的分布式锁系统Chubby作为Paxos实现曾经遭遇到很多坑. 来自Stan ...
- Solr——Windows下部署Solr6.6.0至Tomcat8.5.28(一)
一.window 环境 solr 6.6.3 下载地址 http://archive.apache.org/dist/lucene/solr/ jdk 1.8 tomcat 8.5 本机tomc ...
- windows下安装tomcat8
1.下载tomcat,下载地址:https://tomcat.apache.org/download-80.cgi 2.官网下载下来的tomcat是zip格式,解压到安装目录 3.修改环境变量: CA ...
- Java之ConcurrentHashMap
由于工作中使用到了ConcurrentHashMap,然后查了一波资料,最后整理如下: 1. 描述: ConcurrentHashMap是在Java1.5作为HashTable的替代选择新引入的,是c ...