[Swift]LeetCode89. 格雷編碼 | Gray Code

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-heujaqqj-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

The gray code is a binary numeral system where two successive values differ in only one bit.git

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.github

Example 1:微信

Input: 2
Output: 
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
[0,1,3,2]

Example 2:app

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].

格雷編碼是一個二進制數字系統,在該系統中,兩個連續的數值僅有一個位數的差別。ide

給定一個表明編碼總位數的非負整數 n,打印其格雷編碼序列。格雷編碼序列必須以 0 開頭。編碼

示例 1:spa

輸入: 2
輸出: 
解釋:
00 - 0
01 - 1
11 - 3
10 - 2

對於給定的 n,其格雷編碼序列並不惟一。
例如, 也是一個有效的格雷編碼序列。

00 - 0
10 - 2
11 - 3
01 - 1[0,1,3,2][0,2,3,1]

示例 2:code

輸入: 0
輸出: 格雷編碼序列必須以 0 開頭。編碼總位數爲。[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 }

12mshtm

 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 }
相關文章
相關標籤/搜索