zoukankan      html  css  js  c++  java
  • [Swift]LeetCode869. 重新排序得到 2 的幂 | Reordered Power of 2

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

    Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.

    Return true if and only if we can do this in a way such that the resulting number is a power of 2. 

    Example 1:

    Input: 1
    Output: true
    

    Example 2:

    Input: 10
    Output: false
    

    Example 3:

    Input: 16
    Output: true
    

    Example 4:

    Input: 24
    Output: false
    

    Example 5:

    Input: 46
    Output: true 

    Note:

    1. 1 <= N <= 10^9

    从正整数 N 开始,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。

    如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。 

    示例 1:

    输入:1
    输出:true
    

    示例 2:

    输入:10
    输出:false
    

    示例 3:

    输入:16
    输出:true
    

    示例 4:

    输入:24
    输出:false
    

    示例 5:

    输入:46
    输出:true 

    提示:

    1. 1 <= N <= 10^9

    Runtime: 8 ms
    Memory Usage: 18.6 MB
     1 class Solution {
     2     func reorderedPowerOf2(_ N: Int) -> Bool {
     3         var c:Int = counter(N)
     4         for i in 0..<32
     5         {
     6             if counter(1 << i) == c
     7             {
     8                 return true
     9             }
    10         }
    11         return false
    12     }
    13 
    14     func counter(_ N:Int) ->Int
    15     {
    16         var N = N
    17         var res:Int = 0
    18         while(N > 0)
    19         {
    20             res += Int(pow(10, Double(N % 10)))
    21             N /= 10
    22         }
    23         return res
    24     }
    25 }

    12ms

     1 class Solution {
     2     func reorderedPowerOf2(_ N: Int) -> Bool {
     3          let a = count(N);
     4         for i in 0...31 {
     5             if a.elementsEqual(count(1 << i)) {
     6                 return true;
     7             }
     8         }
     9       return false;
    10 }
    11     
    12     func count(_ N: Int) -> [Int] {
    13         var n = N
    14         var ans =  [Int](repeating: 0, count: 31)
    15         while (n > 0) {
    16             let indx = n % 10;
    17             ans[indx] = ans[indx] + 1;
    18             n /= 10;
    19         }
    20         return ans
    21     }
    22 }

    16ms

     1 class Solution {
     2 
     3     let pows2 = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912]
     4     var dictArr = [Int: [[Character: Int]]]()
     5 
     6     init() {
     7         for n in pows2 {
     8             let s = String(n)
     9             let val = toStringDict(n)
    10             if let arr = dictArr[s.count] {
    11                 var varArr = arr
    12                 varArr.append(val)
    13                 dictArr[s.count] = varArr
    14             } else {
    15                 dictArr[s.count] = [val]
    16             }
    17         }
    18     }
    19 
    20     func reorderedPowerOf2(_ N: Int) -> Bool {
    21         return dictArr[String(N).count]?.contains(toStringDict(N)) ?? false
    22     }
    23 
    24     func toStringDict(_ n: Int) -> [Character : Int] {
    25         let s = String(n)
    26         var dict: [Character : Int] = [:]
    27         for char in s.characters {
    28             if let val = dict[char] {
    29                 dict[char] = val + 1
    30             } else {
    31                 dict[char] = 1
    32             }
    33         }
    34         return dict
    35     }
    36 }

    28ms

     1 class Solution {
     2     func reorderedPowerOf2(_ N: Int) -> Bool {
     3     if N == 1 {return true}
     4     var set = [String]()
     5     let stirng = String("(N)".sorted(by: >))
     6     let max = Int(stirng)!
     7     for i in 1... {
     8         let number = 1 << i
     9         if number > max {break}
    10         set.append(String("(number)".sorted(by: >)))
    11     }
    12      return set.contains(stirng)
    13     }
    14 }
  • 相关阅读:
    iptables
    linux时间同步
    iftop使用
    linux目录结构及定时任务
    awk基本用法
    二、Java面向对象(6)_深入变量
    二、Java面向对象(5)_static修饰符
    二、Java面向对象(4)_构造函数
    二、Java面向对象(3)_类和对象
    二、Java面向对象(2)_软件开发方式
  • 原文地址:https://www.cnblogs.com/strengthen/p/10596985.html
Copyright © 2011-2022 走看看