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 ...
随机推荐
- Java NIO系列教程(二) Channel通道介绍及FileChannel详解
目录: <Java NIO系列教程(二) Channel> <Java NIO系列教程(三) Channel之Socket通道> Channel是一个通道,可以通过它读取和写入 ...
- java打印实心10*10正方形, 空心10*10正方形
public class PrintSquare { public static void main(String[] args) { printSolidSquare(10); System.out ...
- [UE4]根据时间、速度进行插值:Finterp to Constant
一般在“Tick”事件中使用: Current:当前值 Target:期望的目标值 Delta Time:时间变化值. Interp Speed:插值速度 返回值:从“当前值”过渡到“期望的目标值”的 ...
- Python函数式编程-高阶函数、匿名函数、装饰器、偏函数
- Servlet(API)生命周期
一.最上层接口Servlet 查看Servlet接口源码: 有5个方法 访问过程(默认): 1.进行Servlet类加载 当Tomcat容器启动后,服务器寻找应用部署的描述文件(web.xml),从部 ...
- Zookeeper数据查看工具ZooInspector
Zookeeper作为常用的集群协调者组件被广泛应用,尤其是在大数据生态圈中: Zookeeper集群存储各个节点信息,包括:Hadoop.Hbase.Storm.Kafka等等: 二.查询ZK数据的 ...
- 开关Windows休眠功能
在windows休眠的时候会把内存里的数据缓存到硬盘的C:/Hiberfil.sys文件,万一断电能够从中恢复状态,然而这对SSD硬盘损耗很大,如果没必要还是关了吧: 关闭: powercfg -h ...
- node mongodb 案例代码
1.db: var mongoose=require("mongoose"); mongoose.connect('mongodb://localhost:8686/mytest' ...
- 对KVM虚拟机进行cpu pinning配置的方法
这篇文章主要介绍了对KVM虚拟机进行cpu pinning配置的方法,通过文中的各种virsh命令可进行操作,需要的朋友可以参考下 首先需求了解基本的信息 1 宿主机CPU特性查看 使用virsh n ...
- redis永久化存储
redis持久化存储 原因:redis是存放在内存中的,断电会导致数据丢失解决方法:把redis数据进行持久性存储,将其存储在磁盘中. 存储方式:1.RDBRDB中文名为快照/内存快照,Redis按照 ...