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

[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:

输入: 2
输出: [0,1,3,2]
解释:
00 - 0
01 - 1
11 - 3
10 - 2

对于给定的 n,其格雷编码序列并不唯一。
例如,[0,2,3,1] 也是一个有效的格雷编码序列。

00 - 0
10 - 2
11 - 3
01 - 1

示例 2:

输入: 0
输出: [0]
解释: 我们定义格雷编码序列必须以 0 开头。
     给定编码总位数为 n 的格雷编码序列,其长度为 2nn = 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 @ 2018-11-09 17:17  为敢技术  阅读(334)  评论(0编辑  收藏  举报