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 ...
随机推荐
- 学习笔记之k-nearest neighbors algorithm (k-NN)
k-nearest neighbors algorithm - Wikipedia https://en.wikipedia.org/wiki/K-nearest_neighbors_algorith ...
- 面试总结之PYTHON
source code https://github.com/haoran119/interview/tree/master/interview%20summary%20of%20python [ZZ ...
- [UE4]增加开枪冷却时间, Get Time Seconds
Get Time Seconds:游戏开始到现在过去了多少秒
- SpringMVC 请求响应流程
SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMa ...
- Java基础知识_毕向东_Java基础视频教程笔记(22-25 GUI 网络编程 正则)
22天-01-GUIGUI:Graphical User Interface 图形用户接口 Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中CLI:Common lin ...
- 电商系统架构总结2(Redis)
二 Redis缓存 考虑到将来服务器的升级扩展,使用redis代替.net内置缓存是比较理想的选择.redis是非常成熟好用的缓存系统,安装配置非常简单,直接上官网下载安装包 安装启动就行了. 1 ...
- Android软键盘遮挡布局问题;
布局被软键盘遮挡虽然不是什么大问题,但还是比较影响用户体验的:最让人恼火的是当前输入框被软键盘被遮挡,来看一下解决方法: 1.当前输入框被软键盘遮挡,仅把输入框显示出来,不改变整体布局: 设置Mani ...
- java正则表达式替换空格和换行符
public class StringUtil { public static String getStringNoBlank(String str) { if(s ...
- 初级安全入门——XSS注入的原理与利用
XSS的简单介绍 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets,CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者 ...
- call与apply简单介绍
var pet={ word:'...', speak:function(say){ console.log(say+' '+this.word) } } //pet.speak('speak')// ...