zoukankan      html  css  js  c++  java
  • [Swift]LeetCode397. 整数替换 | Integer Replacement

    原文地址:https://www.cnblogs.com/strengthen/p/10305720.html 

    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 }
  • 相关阅读:
    K-Multiple Free set UVA-11246 (容斥原理)
    RAID! UVA-509 (奇偶校验)
    龙芯 fedora28 安装指南
    Kdenlive简明教程-简单的操作
    Kdenlive简明教程-开始
    Irrelevant Elements UVA-1635 (二项式定理)
    指针的指针笔记
    scanf 函数笔记
    printf 函数笔记
    龙芯 3A4000 Fedora28 安装笔记
  • 原文地址:https://www.cnblogs.com/strengthen/p/10305720.html
Copyright © 2011-2022 走看看