首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

89.Gray Code

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;}

};

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180717G1Y2HE00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券