Miner
Algorithm
晚上不要经常熬夜^_^
我回来了, 我这段时间计划了我公众号的路线。
1,始终坚持算法至上。
2,分享我做人工智能(主要是深度学习方面)的实验(会有详细教程),不谈人工智能会怎么,咱就踏踏实实的干事。其次是分享我的前端学习经验。
3,编程语言会转向python和JavaScript,不会放弃C++。
4,会尽量用英语写作
5,不定期分享学习资料。
01
题目:
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
Example 1:
Input: 2Output: [0,1,3,2]Explanation:00 - 001 - 111 - 310 - 2For a given n, a gray code sequence may not be uniquely defined.For example, [0,2,3,1] is also a valid gray code sequence.00 - 010 - 211 - 301 - 1
Example 2:
Input: 0Output: [0]Explanation: We define the gray code sequence to begin with 0. A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1. Therefore, for n = 0 the gray code sequence is [0].
02
解题思路
代码不难理解,大家看代码就可以理解!
C++ version:
Coding
class Solution {
public:
vector grayCode(int n) {
vectorres;
for(int i = 0; i
res.push_back(i ^ i>>1);
return res;}
};
领取专属 10元无门槛券
私享最新 技术干货