首页 > 其他分享 >【刷题笔记】89. Gray Code

【刷题笔记】89. Gray Code

时间:2023-10-19 19:00:58浏览次数:27  
标签:code Gray Code num gray int sequence 89 return

题目

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.

Example 1:

Input: 2
Output: [0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2

For 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 - 0
10 - 2
11 - 3
01 - 1

Example 2:

Input: 0
Output: [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].

题目大意

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。

解题思路

  • 输出 n 位格雷码
  • 格雷码生成规则:以二进制为0值的格雷码为第零项,第一次改变最右边的位元,第二次改变右起第一个为1的位元的左边位元,第三、四次方法同第一、二次,如此反复,即可排列出 n 个位元的格雷码。
  • 可以直接模拟,也可以用递归求解。

参考代码

package leetcode

// 解法一 递归方法,时间复杂度和空间复杂度都较优
func grayCode(n int) []int {
	if n == 0 {
		return []int{0}
	}
	res := []int{}
	num := make([]int, n)
	generateGrayCode(int(1<<uint(n)), 0, &num, &res)
	return res
}

func generateGrayCode(n, step int, num *[]int, res *[]int) {
	if n == 0 {
		return
	}
	*res = append(*res, convertBinary(*num))

	if step%2 == 0 {
		(*num)[len(*num)-1] = flipGrayCode((*num)[len(*num)-1])
	} else {
		index := len(*num) - 1
		for ; index >= 0; index-- {
			if (*num)[index] == 1 {
				break
			}
		}
		if index == 0 {
			(*num)[len(*num)-1] = flipGrayCode((*num)[len(*num)-1])
		} else {
			(*num)[index-1] = flipGrayCode((*num)[index-1])
		}
	}
	generateGrayCode(n-1, step+1, num, res)
	return
}

func convertBinary(num []int) int {
	res, rad := 0, 1
	for i := len(num) - 1; i >= 0; i-- {
		res += num[i] * rad
		rad *= 2
	}
	return res
}

func flipGrayCode(num int) int {
	if num == 0 {
		return 1
	}
	return 0
}

// 解法二 直译
func grayCode1(n int) []int {
	var l uint = 1 << uint(n)
	out := make([]int, l)
	for i := uint(0); i < l; i++ {
		out[i] = int((i >> 1) ^ i)
	}
	return out
}

标签:code,Gray,Code,num,gray,int,sequence,89,return
From: https://blog.51cto.com/u_16110811/7941891

相关文章

  • 189.旋转数组
    目录1.题目法一、利用python自带的reverse函数法二、辅助空间法1.题目给定一个整数数组nums,将数组中的元素向右轮转k个位置,其中k是非负数示例1:输入:nums=[1,2,3,4,5,6,7],k=3输出:[5,6,7,1,2,3,4]解释:向右轮转1步:[7,1,2,3,4,5,6]向右轮转2步:[6,7......
  • 在Visual Studio Code中进行WSL开发
    在VisualStudioCode中轻松进行WSL开发的步骤如下:1.安装VisualStudioCode和WSL扩展:首先,从VisualStudioCode官网下载并安装VisualStudioCode。然后,在VisualStudioCode中安装WSL扩展,该扩展可以让你直接在VSCode中使用WSL作为开发环境[[1]](https://learn.microsoft.com/......
  • !code
    1https://www.douban.com/note/633227132/这是《如是》对一个西川的访谈。在中间一段,回放了西川在中央美院“毕业夜之夜”登台擂鼓朗诵《屈原·少司命》,这种生命力,不是青春的那种火,而是一个成熟的老诗人的内在力量的外显,挺震撼我的,西川的学生们应该是特别有福气的吧,没有见过西川......
  • vscode远程ubuntu,python不识别opencv的函数
    将opencv-python更新到4.8版本以上https://github.com/microsoft/pylance-release/issues/4838......
  • AtCoder Beginner Contest(abc) 308
    B-DefaultPrice题目大意小莫买了n个寿司,现在给出m个寿司的名称和m+1个价格,如果小莫买的其中一个寿司不在这m个寿司之中就用价格m0;请问小莫买的寿司花了多少钱解题思路数据不大,暴力哈希即可;神秘代码#include<bits/stdc++.h>#defineintlonglong#define......
  • 1189
    给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。 示例1:输入:text="nlaebolko"输出:1示例2:输入:text="loonbalxballpoon"输出......
  • leetcode 706 设计哈希映射
    leetcode706.设计哈希映射实现一个hashmapReference题目链接......
  • Atcoder Beginner Contest 324 G Generate Arrays 题解-Treap
    为了更好的阅读体验,请点击这里题目链接套上平衡树板子就能做的很快的题,然后因为是指针存树,因此交换只需要把序列大小较小的挨个拿出来插到相应的地方即可。复杂度\(O(N\log^2N)\)。但是一定要记住不可以直接使用std::swap交换包含带有指针的类的实例(如代码中的Treap类)!......
  • LeetCode142. 环形链表 II
    题目描述给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回null。如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从0开始)。如......
  • Educational Codeforces Round 150 (Rated for Div. 2) B. Keep it Beautiful
    数组\(a=[a_1,a_2,\cdots,a_n]\)被称为是美丽的,如果可以将\([1,x]\)段移到\([x+1,n]\)段后面,\(x\geq0\),数组可以构成非降序。现在有一个数组\(a\)(一开始为空)和\(q\)个询问,第\(i\)个询问给一个正整数\(x_i\)。需要逐步执行以下操作。若\(x_i\)拼接......