【LeetCode】89.Gary Code

Problem:

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

Solution:

理解格雷码和二进制的转换关系这题就很简单了。

格雷码的特点在于相邻两个数字仅有一位不同。题中给出的示例是【00,01,11,10】,原题提示中也说到格雷码的顺序可能不止一种,如【00,10,11,01】亦可,只需满足相邻两数只有一位不同这一条件。

按题示序列规则,对二进制数进行右移运算和异或运算可以得出相应格雷码,如:

// i ^(i>>1)
00(0)^00→00(0)
01(1)^00→01(1)
10(2)^01→11(3)
11(3)^01→10(2)

AC Code(C++):

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         vector<int> seq;
 5         for(int i=0;i<(1<<n);i++){
 6             seq.push_back(i^(i>>1));
 7         }
 8         return seq;
 9     }
10 };
时间: 2024-08-15 06:26:42

【LeetCode】89.Gary Code的相关文章

【LeetCode】89. 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

【一天一道LeetCode】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】Distinct Subsequences

问题:给定两个字符串S,T,对于S,可以删除其中的任意多个(包括0)字符,使其得到T.问有多少种删法可以得到T. 举例分析: S:ababa T: aba dp[i][j] : 表示 S 从0 ~ i - 1,T从0~j - 1,所得到的方法数.i,j 表示长度. 初始条件:dp[i][0] = 1,T为空串,而空串总是任意串的字串.即,将S串的所有字符都删掉,就得到T. 状态转移方程: dp[ i ] [ j ] = dp[ i - 1] [ j - 1] + dp[ i - 1] [ j ]

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【LeetCode】- Length of Last Word(最后一个单词的长度)

[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. 给你一个字符串,设法获取它最后一个单词的长度.如果这个单词不存在,则返回0. [ 分析 : ] A word is defined

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"