• [Swift]LeetCode415. 字符串相加 | Add Strings


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

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

    Note:

    1. The length of both num1 and num2 is < 5100.
    2. Both num1 and num2 contains only digits 0-9.
    3. Both num1 and num2 does not contain any leading zero.
    4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。

    注意:

    1. num1 和num2 的长度都小于 5100.
    2. num1 和num2 都只包含数字 0-9.
    3. num1 和num2 都不包含任何前导零。
    4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

     1 class Solution {
     2     //模拟加法进位
     3     func addStrings(_ num1: String, _ num2: String) -> String {
     4         //字符串数组
     5         var result:[String] = [String]()
     6         //进位标志
     7         var carry:Int = 0
     8         var len1 = num1.count
     9         var len2 = num2.count
    10         while(len1 > 0 || len2 > 0 || carry > 0)
    11         {
    12             var temp:Int = carry
    13             if len1 > 0
    14             {
    15                 len1 -= 1
    16                 var index = num1.index(num1.startIndex,offsetBy: len1)
    17                 var char:Character = num1[index]
    18                 //字符0的ASCII码值 48
    19                 temp = temp + char.toInt() - 48
    20             }
    21             if len2 > 0
    22             {
    23                 len2 -= 1
    24                 var index = num2.index(num2.startIndex,offsetBy: len2)
    25                 var char:Character = num2[index]
    26                 //字符0的ASCII码值 48
    27                 temp = temp + char.toInt() - 48
    28             }
    29             carry = temp / 10
    30             result.append(String(temp % 10))
    31         }
    32         //反转数组
    33         result.reverse()
    34         //数组转字符串
    35         return  result.joined(separator: "")
    36     }
    37 }
    38     //Character扩展代码  
    39     extension Character  
    40     {  
    41         func toInt() -> Int  
    42         {  
    43             var num:Int = Int()
    44             for scalar in String(self).unicodeScalars  
    45             {  
    46                 num = Int(scalar.value)  
    47             }  
    48             return num  
    49         }  
    50     }

    24ms

     1 class Solution {
     2     func addStrings(_ num1: String, _ num2: String) -> String {
     3         
     4         var chars1 = Array(num1.characters)
     5         var chars2 = Array(num2.characters)
     6         
     7         var i = chars1.count - 1
     8         var j = chars2.count - 1
     9         
    10         var result = ""
    11         var carry = 0
    12         
    13         let val0 = Int(Character("0").unicodeScalars.first!.value)
    14         
    15         while i >= 0 || j >= 0 || carry > 0 {
    16             
    17             let val1 = i < 0 ? 0 : Int(chars1[i].unicodeScalars.first!.value) - val0
    18             let val2 = j < 0 ? 0 : Int(chars2[j].unicodeScalars.first!.value) - val0
    19             
    20             let sum = val1 + val2 + carry
    21             
    22             result = String(sum % 10) + result
    23             carry = sum / 10
    24             
    25             i -= 1
    26             j -= 1
    27         }
    28         
    29         return result
    30     }
    31 }

    28ms

     1 class Solution {
     2     func addStrings(_ num1: String, _ num2: String) -> String {
     3         var output = ""
     4         
     5         var num1 = Array(num1.utf8.reversed())
     6         var num2 = Array(num2.utf8.reversed())
     7         
     8         var num1Index = 0
     9         var num2Index = 0
    10         
    11         var c: UInt8 = 0
    12         
    13         while num1Index < num1.count,
    14               num2Index < num2.count {
    15             let n1 = num1[num1Index]
    16             let n2 = num2[num2Index]
    17                 
    18             let v = (n1 - 48) + (n2 - 48) + c
    19             
    20             output.append(String(v % 10))
    21             c = v / 10
    22                 
    23             num1Index += 1
    24             num2Index += 1
    25         }
    26         
    27         while num1Index < num1.count {
    28             let n1 = num1[num1Index]
    29             let v = (n1 - 48) + c
    30             
    31             output.append(String(v % 10))
    32             c = v / 10
    33             
    34             num1Index += 1
    35         }
    36         
    37         while num2Index < num2.count {
    38             let n2 = num2[num2Index]
    39             let v = (n2 - 48) + c
    40             
    41             output.append(String(v % 10))
    42             c = v / 10
    43             
    44             num2Index += 1
    45         }
    46         
    47         if c != 0 {
    48             output.append(String(c))
    49         }
    50         
    51         return String(output.reversed())
    52     }
    53 }

    32ms

     1 class Solution {
     2     private let charToInt : [Character : Int] = [
     3         "0" : 0, "1" : 1, "2" : 2, "3" : 3, "4" : 4,
     4         "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9
     5     ]
     6     
     7     func addStrings(_ num1: String, _ num2: String) -> String {
     8         let chars1 = Array(num1.characters)
     9         let chars2 = Array(num2.characters)
    10         var i = chars1.count - 1
    11         var j = chars2.count - 1
    12         var carry : Int = 0, sum : Int = 0
    13         var rlt: String = ""
    14 
    15         while i >= 0 || j >= 0 {
    16             let n1 = i >= 0 ? charToInt[chars1[i]] : 0
    17             let n2 = j >= 0 ? charToInt[chars2[j]] : 0
    18             sum = n1! + n2! + carry
    19             carry = sum / 10
    20             rlt = "(sum % 10)" + rlt
    21             i -= 1
    22             j -= 1
    23         }
    24         if carry > 0 { // check the last carry for entire string;
    25             rlt = "(carry)" + rlt
    26         }
    27         return rlt
    28     }
    29 }

    36ms

     1 class Solution {
     2     func addStrings(_ num1: String, _ num2: String) -> String {
     3         var it1 = num1.count - 1
     4         var it2 = num2.count - 1
     5         var nn1 = Array(num1)
     6         var nn2 = Array(num2)
     7         var rem = 0
     8         var res: String = ""
     9         
    10         while it1 >= 0 || it2 >= 0 {
    11             defer { it1 -= 1; it2 -= 1 }
    12             let d1 = it1 >= 0 ? (toNum(nn1[it1]) ?? 0) : 0
    13             let d2 = it2 >= 0 ? (toNum(nn2[it2]) ?? 0) : 0
    14             let num = rem + d1 + d2
    15             res.append(toChar(num % 10))
    16             rem = num / 10
    17         }
    18         if rem > 0 {
    19             res.append("1")
    20         }
    21         return String(res.reversed())
    22     }
    23     
    24     let chDigits = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9]
    25     let digitsCh = [0:"0",1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9"]
    26     
    27     func toNum(_ char: Character) -> Int? {
    28          return chDigits[String(char)]
    29     }
    30     
    31     func toChar(_ n: Int) -> String {
    32         return digitsCh[n]!
    33     }
    34 }

    36ms

     1 class Solution {
     2     func addStrings(_ num1: String, _ num2: String) -> String {
     3         var result = ""
     4         
     5         var arr1: [UnicodeScalar] = num1.unicodeScalars.reversed()
     6         var arr2: [UnicodeScalar] = num2.unicodeScalars.reversed()
     7         var carry = 0
     8         var index = 0
     9         
    10         while index < arr1.count || index < arr2.count {
    11             var n1 = 0
    12             var n2 = 0
    13             
    14             if index < arr1.count {
    15                 let ch1 = arr1[index]
    16                 n1 = Int(ch1.value) - Int("0".unicodeScalars.first!.value)
    17             }
    18             if index < arr2.count {
    19                 let ch2 = arr2[index]
    20                 n2 = Int(ch2.value) - Int("0".unicodeScalars.first!.value)
    21             }
    22             
    23             let sum = n1 + n2 + carry
    24             result = String(sum % 10) + result
    25             carry = sum / 10
    26             index += 1
    27         }
    28         
    29         if carry > 0 {
    30             result = String(carry) + result
    31         }
    32         
    33         return result
    34     }
    35 
    36 }

    44ms

     1 class Solution {
     2     func addStrings(_ num1: String, _ num2: String) -> String {
     3         
     4         let nums1 = Array(num1.characters).map { 
     5             Int($0.unicodeScalars.first!.value - Character("0").unicodeScalars.first!.value)
     6         }
     7         let nums2 = Array(num2.characters).map { 
     8             Int($0.unicodeScalars.first!.value - Character("0").unicodeScalars.first!.value)
     9         }
    10 
    11         var i = nums1.count - 1
    12         var j = nums2.count - 1
    13         var carry = 0
    14         var result = ""
    15         
    16         while i >= 0 || j >= 0 || carry > 0 {
    17             
    18             let num1 = i >= 0 ? nums1[i] : 0
    19             let num2 = j >= 0 ? nums2[j] : 0
    20             
    21             let sum = num1 + num2 + carry
    22             result = String(sum % 10) + result
    23             carry = sum / 10
    24             
    25             i -= 1
    26             j -= 1
    27         }
    28         
    29         return result
    30     }
    31 }

    44ms

     1 import Foundation
     2 
     3 class Solution {
     4     
     5     func addStrings(_ num1: String, _ num2: String) -> String {
     6         var result = ""
     7         
     8         var arr1: [UnicodeScalar] = num1.unicodeScalars.reversed()
     9         var arr2: [UnicodeScalar] = num2.unicodeScalars.reversed()
    10         var carry = 0
    11         var index = 0
    12         
    13         while index < arr1.count || index < arr2.count {
    14             var n1 = 0
    15             var n2 = 0
    16             
    17             if index < arr1.count {
    18                 let ch1 = arr1[index]
    19                 n1 = Int(ch1.value) - Int("0".unicodeScalars.first!.value)
    20             }
    21             if index < arr2.count {
    22                 let ch2 = arr2[index]
    23                 n2 = Int(ch2.value) - Int("0".unicodeScalars.first!.value)
    24             }
    25             
    26             let sum = n1 + n2 + carry
    27             result = String(sum % 10) + result
    28             carry = sum / 10
    29             index += 1
    30         }
    31         
    32         if carry > 0 {
    33             result = String(carry) + result
    34         }
    35         
    36         return result
    37     }
    38 }
  • 相关阅读:
    js 自动下载函数
    集群中用Memcached来实现session共享
    PDO防注入原理分析以及使用PDO的注意事项
    侧边栏
    helloworld
    angularjs 获取地址传参
    ionic 上拉加载更多&瀑布流加载&滚动到底部加载更多 主意事项
    亿级Web系统搭建——单机到分布式集群
    php 模拟表单提交
    R语言curve绘图函数
  • 原文地址:https://www.cnblogs.com/strengthen/p/9783317.html
Copyright © 2020-2023  润新知