• [Swift]LeetCode504. 七进制数 | Base 7


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

    Given an integer, return its base 7 string representation.

    Example 1:

    Input: 100
    Output: "202"
    

     Example 2:

    Input: -7
    Output: "-10"
    

     Note: The input will be in range of [-1e7, 1e7].


     给定一个整数,将其转化为7进制,并以字符串形式输出。

    示例 1:

    输入: 100
    输出: "202"
    

    示例 2:

    输入: -7
    输出: "-10"
    

    注意: 输入范围是 [-1e7, 1e7] 。


     16ms

     1 class Solution {
     2     func convertToBase7(_ num: Int) -> String {
     3         if num == 0 {return "0"}
     4         var res:[Int] = [Int]()
     5         //取绝对值
     6         var number:Int = abs(num)
     7         while(number != 0)
     8         {
     9             var temp:Int = number % 7
    10             number /= 7
    11             //在位置0处插入
    12             res.insert(temp,at: 0)
    13         }
    14         //数组转字符串
    15         let dataInt = res.map(String.init)      
    16         var str:String = dataInt.joined(separator: "")
    17         if num < 0
    18         {
    19             str = "-" + str
    20         }
    21         return str
    22     }
    23 }

    8ms

     1 class Solution {
     2     func convertToBase7(_ num: Int) -> String {
     3         var input = abs(num)
     4 
     5         var result = ""
     6 
     7         if num == 0 {
     8             return "0"
     9         }
    10 
    11         while input >= 1 {
    12             result = "(input % 7)" + result
    13             input = input / 7
    14         }
    15 
    16         if num < 0 {
    17             result = "-" + result
    18         }
    19 
    20         return result
    21     }
    22 }

    12ms

     1 class Solution {
     2     func convertToBase7(_ num: Int) -> String {
     3         if num == 0 { return "0" }
     4         let number = abs(num)
     5         var result = ""
     6         var x = number
     7         while x > 0 {
     8             let rem = x % 7
     9             result = "(rem)(result)"
    10             x = x / 7
    11         }
    12         
    13         return num < 0 ? "-(result)" : result
    14     }
    15 }

    20ms

     1 class Solution {
     2     func convertToBase7(_ num: Int) -> String {
     3         if num < 0 {
     4             return "-" + convertToBase7(-num)
     5         }
     6 
     7         var res = ""
     8         var num = num
     9 
    10         while num >= 7 {
    11             res = String(num % 7) + res
    12             num /= 7
    13         }
    14         res = String(num) + res
    15 
    16         return res
    17     }
    18 }

    24ms

     1 class Solution {
     2     func convertToBase7(_ num: Int) -> String {
     3         guard num != 0 else {
     4             return "0"
     5         }
     6         var num = num
     7         var flag = true
     8         if num < 0 {
     9             flag = false
    10             num = -num
    11         }
    12         var result = ""
    13         while num > 0 {
    14             result = "(num % 7)" + result
    15             num = num / 7
    16         }
    17         return flag ? result : ("-" + result)
    18     }
    19 }

      

  • 相关阅读:
    MySql 免安装配置
    MySql 语句优化的几个小建议
    Sharding-JDBC 公共表
    Sharding-JDBC 垂直分库
    Sharding-JDBC 水平分库
    Vuejs基础使用
    webpack配置开发环境和生产环境
    webpack常见应用
    webpack-用loader加载css样式、图片、字体、数据
    webpack核心概念-模式/loader/插件及其他
  • 原文地址:https://www.cnblogs.com/strengthen/p/9809768.html
Copyright © 2020-2023  润新知