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

[Swift]LeetCode397. 整数替换 | Integer Replacement

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

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

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

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

Given a positive integer n and you can do operations as follow: 

  1. If n is even, replace n with n/2.
  2. If n is odd, you can replace n with either n + 1 or n - 1

What is the minimum number of replacements needed for n to become 1? 

Example 1:

Input:
8

Output:
3

Explanation:
8 -> 4 -> 2 -> 1 

Example 2:

Input:
7

Output:
4

Explanation:
7 -> 8 -> 4 -> 2 -> 1
or
7 -> 6 -> 3 -> 2 -> 1

给定一个正整数 n,你可以做如下操作:

1. 如果 是偶数,则用 n / 2替换 n
2. 如果 是奇数,则可以用 n + 1n - 1替换 n
变为 1 所需的最小替换次数是多少?

示例 1:

输入:
8

输出:
3

解释:
8 -> 4 -> 2 -> 1

示例 2:

输入:
7

输出:
4

解释:
7 -> 8 -> 4 -> 2 -> 1
或
7 -> 6 -> 3 -> 2 -> 1

8ms
 1 class Solution {
 2     func integerReplacement(_ n: Int) -> Int {
 3         var step = 0
 4         var n = n
 5         
 6         while n > 1 {
 7             if n % 2 == 0{
 8                 n /= 2
 9             }
10             else {
11                 if n > 3 && n & 3 == 3 {
12                     n += 1
13                 }
14                 else {
15                     n -= 1
16                 }
17             }
18             step += 1
19         }
20 
21         return step
22     }
23     
24     func integerReplacement(_ n: Int, _ step: Int) -> Int {
25         if n == 1 {
26             return step
27         }
28         
29         if n % 2 == 0 {
30             return integerReplacement(n / 2, step + 1)
31         }
32         
33         let step1 = integerReplacement(n+1, step+1)
34         let step2 = integerReplacement(n-1, step+1)
35         
36         return min(step1, step2)
37     }
38 }

12ms

 1 class Solution {
 2     func integerReplacement(_ n: Int) -> Int {
 3         var fin = n
 4         var steps = 0
 5         while fin != 1 {
 6             if fin % 2 == 0 {
 7                 fin = fin / 2
 8                 steps += 1
 9             } else {
10                 steps += 1
11                 if (fin - 1) / 2 % 2 == 0 || fin - 1 == 2 {
12                     fin -= 1
13                 } else {
14                     fin += 1
15                 }
16             }
17         }
18         return steps
19     }
20 }

16ms

 1 class Solution {
 2     typealias Node = (val: Int, len: Int)
 3    
 4     func insert(_ c: inout [Node], _ n: Node) {
 5         if c.count == 0 { c.append(n); return }
 6         for i in 0..<c.count {
 7             if c[i].len > n.len { c.insert(n, at: i); return }
 8         }
 9         c.append(n)
10     }
11     
12     func integerReplacement(_ n: Int) -> Int {
13         var c: [Int:Int] = [:]
14         var d: [Int] = [n]
15         c[n] = 0
16         if n == 1 { return 0 }
17         while true {
18             let dold = d
19             d = []
20             for k in dold {
21                 let len = c[k]!
22                 if k % 2 == 1 {
23                     // odd
24                     let next1 = k - 1
25                     let next2 = k + 1
26                     if next1 == 1 || next2 == 1 { return len + 1 }
27                     if c[next1] == nil { c[next1] = len + 1 }
28                     else { c[next1] == min(len + 1, c[next1]!)}
29                     if c[next2] == nil { c[next2] = len + 1 }
30                     else { c[next2] == min(len + 1, c[next2]!)}
31                     if !d.contains(next1) { d.append(next1) }
32                     if !d.contains(next2) { d.append(next2) }
33                 } else {
34                     let next = k/2
35                     if next == 1 { return len + 1 }
36                     if c[next] == nil { c[next] = len + 1 }
37                     else { c[next] == min(len + 1, c[next]!)}
38                     if !d.contains(next) { d.append(next) }
39                 }
40             }
41         }
42         
43         return 0
44     }
45 }

32ms

 1 class Solution {
 2     func integerReplacement(_ n: Int) -> Int {
 3         if n <= 1 {
 4             return 0
 5         }
 6         var m = n
 7         var count = 0
 8         while m != 1 {
 9             if (m & 3) == 3 && m != 3 {
10                 m += 1
11             }else if (m & 1) == 1 {
12                 m -= 1
13             }else {
14                 m = m >> 1
15             }
16             count += 1
17         }
18         return  count
19     }
20 }

40ms

 1 class Solution {
 2     func integerReplacement(_ n: Int) -> Int {
 3         if n == 1 {return 0}
 4         if n % 2 == 0 {return 1 + integerReplacement(n / 2)}
 5         else
 6         {
 7             var t:Int = n
 8             return 2 + min(integerReplacement((t + 1) / 2), integerReplacement((t - 1) / 2))
 9         }
10 
11     }
12 }

 

posted @ 2019-01-22 19:41  为敢技术  阅读(270)  评论(0编辑  收藏  举报