Swift算法:Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note:

  • The numbers can be arbitrarily large and are non-negative.
  • Converting the input string to integer is NOT allowed.
  • You should NOT use internal library such as BigInteger.
class Solution {
    func multiply(num1: String, _ num2: String) -> String {
        if num1 == "" || num2 == "" {
            return ""
        }
        
        var n1 = num1.characters.map { (c) -> Int in
            return Int("\(c)")!
        }
        var n2 = num2.characters.map { (c) -> Int in
            return Int("\(c)")!
        }
        n1 = n1.reverse()
        n2 = n2.reverse()
        
        var n3:[Int] = []
        for i in 0 ..< n2.count {
            let fix = Array(count: i, repeatedValue: 0)
            n3 = addTwoNumber(n3, a2: fix + mutiplySimply(n1, num2: n2[i]))
        }
        
        var result: String = n3.reverse().reduce("") { (s, num) -> String in
            return s + "\(num)"
        }
        
        while result.hasPrefix("0") {
            result.removeAtIndex(result.startIndex)
        }
        if result == "" {
            result = "0"
        }
        
        return result
    }
    
    //倒序输入 倒序输出
    func addTwoNumber(a1: [Int], a2: [Int]) -> [Int] {
        let c1 = a1.count
        let c2 = a2.count
        let c3 = c1<c2 ? c1:c2
        
        var a3: [Int] = []
        
        var carry = 0
        for i in 0 ..< c3 {
            a3.append(a1[i]+a2[i]+carry)
            if a3[i] >= 10 {
                a3[i] = a3[i]%10
                carry = 1
            } else {
                carry = 0
            }
        }
        
        if c1 < c2 {
            for i in c1 ..< c2 {
                a3.append(a2[i]+carry)
                if a3[i] >= 10 {
                    a3[i] = a3[i]%10
                    carry = 1
                } else {
                    carry = 0
                }
            }
        } else if c1 > c2 {
            for i in c2 ..< c1 {
                a3.append(a1[i]+carry)
                if a3[i] >= 10 {
                    a3[i] = a3[i]%10
                    carry = 1
                } else {
                    carry = 0
                }
            }
        }
        
        if carry == 1 {
            a3.append(carry)
        }
        return a3
    }
    
    func mutiplySimply(num1: [Int], num2: Int) -> [Int] {
        let c1 = num1.count
        var num3: [Int] = []
        
        var carry = 0
        for i in 0 ..< c1 {
            num3.append(num1[i]*num2+carry)
            if num3[i] >= 10 {
                carry = num3[i]/10
                num3[i] = num3[i]%10
            } else {
                carry = 0
            }
        }
        
        if carry != 0 {
            num3.append(carry)
        }
        return num3
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,149评论 0 23
  • 长期: 1.对于自己陌生的领域,先从简单的模仿开始,只要老老实实的学习,行动。后面的收获是“聪明人”体现不到的。2...
    WayneLai阅读 418评论 1 0
  • 五月一日的 1:走路14520步,完成每日一万步目标。 2:洗了春天的衣服,夏天的衣服拿出来。 3:看完电视剧潜伏
    心境如花阅读 124评论 0 0