【Leetcode】【Medium】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 - 2
Note:
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.
解题思路:
这道题相当于"穷举"n个0/1排列组合,涉及到排列组合,通常可以先考虑回溯法。
设ret表示返回的数字列表,ret初始为[0,],有两种思路:
1、循环遍历ret列表n次,每次对ret中每一个元素使用"<<"左移运算,并将左移运算后的结果加1,加入列表当中;
即{0}->{0, 1}->{00, 10, 01, 11}->{000, 100, 010, 110, 101, 011, 111}->...
即{0}->{0, 1}->{0, 2, 1, 3}->{0, 4, 2, 6, 1, 5, 3, 7} ...
2、取出ret中已有元素的值,并将其增加,将新值插入ret中,循环n次:
{0} 取出每个元素加入1,并将结果加入列表 ->{0, 1} ->{0, 1}
{0, 1} 取出每个元素加入10,并将结果加入列表 ->{0, 1, 10, 11} ->{0, 1, 2, 3}
{10, 11} 取出每个元素加入100,并将结果加入列表 ->{0, 1, 10, 11, 100, 101, 110, 111} ->{0, 1, 2, 3, 4, 5, 6, 7}
但是这个该死的题目要求必须要按照Leetcode给定的顺序排列数字,就是必须{0, 1, 3, 2};
因此只能使用第二种方法,并且倒序取出已有元素;
代码:
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result(, );
int inc = ; while (n--) {
int s = result.size();
for (int j = s - ; j >= ; --j)
result.push_back(result[j] + inc);
inc <<= ;
} return result;
}
};
【Leetcode】【Medium】Gray Code的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
随机推荐
- 实验Complex
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex ...
- openerp学习笔记 数据合法性约束(对象约束+数据库约束)
#检测同一时间段内是否存在相同的请假单,False 是存在,不允许创建 def _check_date(self, cr, uid, ids): for rec in self.b ...
- goLang冒泡
// test project main.gopackage main import ( "fmt") func main() { var a = [10]int{1, ...
- WPF实现无刷新动态切换多语言(国际化)
1. 在WPF中国际化使用的是 .xaml文件的格式 如图:Resource Dictionary (WPF) 2. 创建默认的语言文件和其他语言文件 这里以英语为默认语言,新建一个 Resource ...
- hibernate 学习笔记2
1.Criteria查询接口适用于组合多个限制条件来搜索一个查询集. 要使用Criteria,需要遵循以下步骤: *创建查询接口: Criteria criteria=session.createCr ...
- java.net.SocketException四大异常解决方案---转
java.net.SocketException如何才能更好的使用呢?这个就需要我们先要了解有关这个语言的相关问题.希望大家有所帮助.那么我们就来看看有关java.net.SocketExceptio ...
- jQuery插件 -- UI插件Tabs Widget 1.10
最近将tabs(标签页)整合到Admin LTE( 一个完全响应式管理模板,基于Bootstrap3的框架.)中使用.过程中走了不少弯路,原因是没有注意版本问题,jquery ui tabs 每次升级 ...
- 微信小程序准备阶段。(一)
--知识储备--HTML+JS+CSS (一)下载一个微信web开发工具(后续代码都会写在这里边)附上链接:https://mp.weixin.qq.com/debug/wxadoc/dev/devt ...
- Linux中的叹号命令
在shell环境下操作,需要积累点快捷输入的小技巧: 最常用的技巧恐怕就是Tab自动补全以及上方向键来回退上几条历史命令了,这些对于csh,bash,ksh,zsh都适用. 最近还找到一种快速回退上一 ...
- 多个tomcat配置
在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...