• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode415.字符串相加|AddStrings

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap