有牺牲多壮志,敢教日月换新天。为

[Swift]LeetCode89. 格雷编码 | Gray Code

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9936181.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 开头。

示例 1:

[0,1,3,2]
[0,2,3,1]

示例 2:

[0]
解释: 我们定义

     给定
 n 的格雷编码序列,其长度为 2n
当 n = 0 时,长度为 20 = 1。
     因此,当 n = 0 时,其格雷编码序列为 [0]。

8ms
复制代码
 1 class Solution {
 2     func grayCode(_ n: Int) -> [Int] {
 3         var result = [Int]()
 4         var i: Int = 0
 5         while i < (1 << n) {
 6             result.append( i ^ (i >> 1))
 7             i += 1
 8         }
 9         return result
10     }
11 }
复制代码

8ms
复制代码
1 class Solution {
2     func grayCode(_ n: Int) -> [Int] {
3         var result = [Int]()
4         for i in 0..<1 << n {
5             result.append((i >> 1) ^ i)
6         }
7         return result
8     }
9 }
复制代码

12ms

复制代码
 1 class Solution {
 2     func grayCode(_ number: Int) -> [Int] {
 3         guard number > 0 else {
 4             return [0]
 5         }
 6         var result: [Int] = [0, 1]
 7         for idx in 1..<number {
 8             for iidx in (0...result.count - 1).reversed() {
 9                 result.append(result[iidx] + 1 << idx)
10             }
11         }
12         return result
13     }
14 }
复制代码

16ms

复制代码
 1 class Solution {
 2     func grayCode(_ n: Int) -> [Int] {
 3         if n == 0 {
 4             return [0]
 5         }
 6         
 7         if n == 1 {
 8             return [0, 1]
 9         }
10         
11         var res = [0, 1]
12         
13         for i in 1..<n {
14             var result: [Int] = []
15             for val in res.reversed() {
16                 result.append(val + (1<<i))
17             }
18             res = res + result
19         }
20         
21         return res
22     }
23 }
复制代码

24ms

复制代码
 1 class Solution {
 2     func grayCode(_ n: Int) -> [Int] {
 3         if n == 0 {
 4             return [0]
 5         }
 6         var result = [Array<Int>(repeating: 0, count: n)];
 7         for i in 0 ..< n {
 8             let count = result.count
 9             for j in stride(from: count - 1, to: -1, by: -1) {
10                 var temp = result[j]
11                 temp[n - i - 1] = 1
12                 result.append(temp)
13             }
14         }
15         var realResult = Array<Int>()
16         for tempArr in result {
17             realResult.append(binTodec(number: tempArr))
18         }
19         return realResult
20     }
21     
22     func binTodec(number num: Array<Int>) -> Int {
23         var sum: Int = 0
24         for c in num {
25             sum = sum * 2 + c
26         }
27         return sum
28     }
29 }
复制代码

 

posted @   为敢技术  阅读(348)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°