[Swift]LeetCode989. 数组形式的整数加法 | Add to Array-Form of Integer
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10361491.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
For a non-negative integer X
, the array-form of X
is an array of its digits in left to right order. For example, if X = 1231
, then the array form is [1,2,3,1]
.
Given the array-form A
of a non-negative integer X
, return the array-form of the integer X+K
.
Example 1:
Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234
Example 2:
Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455
Example 3:
Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021
Example 4:
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000
Note:
1 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- If
A.length > 1
, thenA[0] != 0
对于非负整数 X
而言,X
的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X = 1231
,那么其数组形式为 [1,2,3,1]
。
给定非负整数 X
的数组形式 A
,返回整数 X+K
的数组形式。
示例 1:
输入:A = [1,2,0,0], K = 34 输出:[1,2,3,4] 解释:1200 + 34 = 1234
解释 2:
输入:A = [2,7,4], K = 181 输出:[4,5,5] 解释:274 + 181 = 455
示例 3:
输入:A = [2,1,5], K = 806 输出:[1,0,2,1] 解释:215 + 806 = 1021
示例 4:
输入:A = [9,9,9,9,9,9,9,9,9,9], K = 1 输出:[1,0,0,0,0,0,0,0,0,0,0] 解释:9999999999 + 1 = 10000000000
提示:
1 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- 如果
A.length > 1
,那么A[0] != 0
1 class Solution { 2 3 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 4 let kDigits = digits(K) 5 let result = add(A, kDigits) 6 return result 7 } 8 9 func digits(_ value: Int) -> [Int] { 10 var result = [Int]() 11 12 var value = value 13 while value > 0 { 14 result.append(value%10) 15 value /= 10 16 } 17 18 return result.reversed() 19 } 20 21 func add(_ values1: [Int], _ values2: [Int]) -> [Int] { 22 var result = [Int]() 23 var index1 = values1.count - 1 24 var index2 = values2.count - 1 25 var carry = 0 26 27 while index1 >= 0 || index2 >= 0 || carry > 0 { 28 let value1 = (index1 >= 0) ? values1[index1] : 0 29 let value2 = (index2 >= 0) ? values2[index2] : 0 30 let sum = value1 + value2 + carry 31 result.append(sum%10) 32 carry = sum / 10 33 34 index1 -= 1 35 index2 -= 1 36 } 37 38 return result.reversed() 39 } 40 }
316ms
1 class Solution { 2 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 3 var output: [Int] = [] 4 var current: Int = 0 5 var kTmp: Int = K 6 7 var i: Int = A.count - 1 8 while i >= 0 || kTmp > 0 { 9 if i >= 0 { 10 current += A[i] 11 } 12 if kTmp > 0 { 13 current += kTmp % 10 14 kTmp /= 10 15 } 16 output.append(current % 10) 17 current /= 10 18 i -= 1 19 } 20 if current > 0 { 21 output.append(current) 22 } 23 output.reverse() 24 return output 25 } 26 }
324ms
1 class Solution { 2 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 3 guard K != 0 else { 4 return A 5 } 6 let ka = String(K).compactMap {Int("\($0)")} 7 8 guard A.count != 0 else { 9 return ka 10 } 11 12 let add: [Int] 13 var result: [Int] 14 if ka.count < A.count { 15 add = Array(ka.reversed()) 16 result = Array(A.reversed()) 17 } else { 18 add = Array(A.reversed()) 19 result = Array(ka.reversed()) 20 } 21 var acc = 0 22 var index = 0 23 while (acc > 0) || (index < add.count) { 24 let s1 = index < add.count ? add[index] : 0 25 let s2 = index < result.count ? result[index] : 0 26 let sum = s1 + s2 + acc 27 acc = sum / 10 28 if index < result.count { 29 result[index] = sum % 10 30 } else { 31 result.append(sum % 10) 32 } 33 index += 1 34 } 35 return result.reversed() 36 } 37 }
336ms
1 class Solution { 2 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 3 var A = A 4 var n:Int = A.count 5 var c:Int = K 6 for i in stride(from:A.count - 1,through:0,by:-1) 7 { 8 A[i] += c 9 c = A[i] / 10 10 A[i] %= 10 11 } 12 var ret:[Int] = [Int]() 13 while(c > 0) 14 { 15 ret.insert(c%10,at:0) 16 c /= 10 17 } 18 for v in A 19 { 20 ret.append(v) 21 } 22 return ret 23 } 24 }
340ms
1 class Solution { 2 private func IntToArra(_ num: Int) -> [Int] { 3 var kArr = [Int]() 4 var p = 10 5 while true { 6 kArr.append(num % p / (p / 10) ) 7 if num % p == num { break } 8 p *= 10 9 } 10 return kArr 11 } 12 13 private func longPlus(_ a: [Int], _ b: [Int]) -> [Int] { 14 let maxA, minA: [Int] 15 (maxA, minA) = a.count >= b.count ? (a, b) : (b, a) 16 var inc: Int = 0 17 var res = [Int](repeating: 0, count: maxA.count + 1) 18 for i in stride(from: maxA.count, through: 0, by: -1) { 19 let maxInd = i - 1 20 let minInd = i - (maxA.count - minA.count) - 1 21 let maxVal = maxInd >= 0 ? maxA[maxInd] : 0 22 let minVal = minInd >= 0 ? minA[minInd] : 0 23 let sum = maxVal + minVal + inc 24 if sum >= 10 { 25 res[i] = sum % 10 26 inc = 1 27 } else { 28 res[i] = sum 29 inc = 0 30 } 31 } 32 if res[0] == 0 { 33 return Array(res[1..<res.count]) 34 } 35 return res 36 } 37 38 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 39 return longPlus(A, IntToArra(K).reversed()) //fix 40 } 41 }
376ms
1 class Solution { 2 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 3 var a = A 4 var k = K 5 6 var i = 0 7 var carry = 0 8 9 while i < a.count || k > 0 { 10 var ai = 0, bi = 0 11 var aIndex = -1 12 13 if i < a.count { 14 aIndex = a.count - (i + 1) 15 ai = a[aIndex] 16 } 17 if k > 0 { 18 bi = k > 9 ? k % 10 : k 19 } 20 21 let c = ai + bi + carry 22 carry = c / 10 23 24 if c > 9 { 25 if aIndex >= 0 { 26 a[aIndex] = c % 10 27 } else { 28 a.insert(c % 10, at: 0) 29 } 30 31 } else { 32 if aIndex >= 0 { 33 a[aIndex] = c 34 } else { 35 a.insert(c, at: 0) 36 } 37 } 38 39 i += 1 40 k /= 10 41 } 42 43 if carry > 0 { 44 a.insert(carry, at: 0) 45 } 46 47 return a 48 49 } 50 }
408ms
1 class Solution { 2 func addToArrayForm(_ A: [Int], _ K: Int) -> [Int] { 3 var aIndex = A.count - 1 4 var k = K 5 var c = 0 6 var result = [Int]() 7 while (aIndex > -1 || k > 0 || c > 0) { 8 var sum = c 9 if aIndex > -1 { 10 sum += A[aIndex] 11 aIndex -= 1 12 } 13 14 if k > 0 { 15 sum += k % 10 16 k = k / 10 17 } 18 19 c = sum / 10 20 result.insert(sum%10, at: 0) 21 } 22 23 return result 24 } 25 }