首页 > 其他分享 >89. Gray Code

89. Gray Code

时间:2023-03-07 13:05:55浏览次数:35  
标签:Gray code sequence ## 异或 Code 89 result gray

##题目
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.

##思路
这道题目,答案不唯一,本文给出其中一种解法,采用按位操作,即异或的使用,实现巧妙,理解简单
##代码

class Solution {
public:
    vector<int> grayCode(int n) {
    vector<int> result;
    for (int i = 0; i < 1<<n; i++) result.push_back(i ^ i>>1);//异或操作
    return result;
    }
};

标签:Gray,code,sequence,##,异或,Code,89,result,gray
From: https://blog.51cto.com/u_15996214/6105910

相关文章

  • 自己用的Code::Blocks IDE设置记录(不时更新)
    Code::Blocks2023.01全中文汉化-优化版,下载地址见小龙Dev-C++作者的这篇blog:https://blog.csdn.net/yandex/article/details/128970278 中文乱码:  编辑器......
  • 08、VsCode配置gcc
    1.1下载gcchttps://github.com/niXman/mingw-builds-binaries/releases1.2配置环境变量bin目录略1.3vscode安装c/c++插件调试运行会生成c_cpp_properties.json和ta......
  • 一张图看懂CodeArts Repo 6大特性,带你玩转代码托管服务
    华为云CodeArtsRepo是华为全栈自研的代码托管服务,基于Git提供分布式代码管理和协同开发能力,包括成员管理、权限控制、代码托管、代码检查、代码审核、代码追溯、持续集成等......
  • how to get the native code of Promise In Chrome All In One
    howtogetthenativecodeofPromiseInChromeAllInOnetypeofPromise;//'function'Promise//ƒPromise(){[nativecode]}V8Enginehttps://github.......
  • LEETCODE 1096. 花括号展开 II
    这个题把题目中的表达式并列关系看做是求和,把相接看做是求积,那么求解整个表达式的过程可以类比于求解中缀表达式的过程。然后利用两个栈一个存运算符,一个存运算对象classSo......
  • LeetCode1024 -- 贪心
    1.题目描述这题题意感觉说的不是很清楚,容易让人产生歧义!其实题意很简单,给你一个链表head,你深拷贝它,然后返回即可,注意不能修改原链表/*//DefinitionforaNode.cl......
  • VS插件CodeRush全新发布v22.2.4——改进对VS 17.5的支持
    CodeRush是一个强大的VisualStudio®.NET插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验。CodeRush能帮助你以极高的效率创建和维护源代码。Consume-firs......
  • #yyds干货盘点# LeetCode面试题:组合总和 II
    1.简述:给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只......
  • 【LeeCode】
    【题目描述】给你一个非负整数 ​​x​​​ ,计算并返回 ​​x​​ 的 算术平方根 。由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去。注意:不允许使用......
  • Atcoder-ABC291 "Teleporter and Closed off" 动态DP版
    题目地址题意:在一个DAG图中,点i只有最多m条出边连向i+1~i+m(m<=10),边权均为1。对于\(k\in[2,n-1]\),依次输出当点k被删除时1到n的最短路。分析:标准做法无非就是预......