码迷,mamicode.com
首页 > 其他好文 > 详细

Multiply Strings

时间:2014-10-09 16:02:38      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   java   for   sp   div   

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.

答案

public class Solution {
    public String multiply(String num1, String num2)
    {
        if (num1.length() < num2.length())
        {
            return multiply(num2, num1);
        }
        List<Integer> p;
        List<Integer> result;
        p = new ArrayList<Integer>(num1.length() + num2.length());
        result = new ArrayList<Integer>(num1.length() + num2.length());
        result.add(0);
        for (int i = num1.length() - 1; i >= 0; i-- )
        {
            p.add(Integer.parseInt(num1.substring(i, i + 1)));
        }
        for (int i = num2.length() - 1; i >= 0; i-- )
        {
            int times = Integer.parseInt(num2.substring(i, i + 1));
            if (times > 0)
            {
                int pIndex = 0;
                int rIndex = 0;
                int carry = 0;
                for (; rIndex < result.size(); pIndex++ , rIndex++ )
                {
                    int num = result.get(rIndex) + times * p.get(pIndex) + carry;
                    carry = num / 10;
                    result.set(rIndex, num % 10);
                }
                for (; pIndex < p.size(); pIndex++ )
                {
                    int num = times * p.get(pIndex) + carry;
                    carry = num / 10;
                    result.add(num % 10);
                }
                if (carry > 0)
                {
                    result.add(carry);
                }
            }
            p.add(0, 0);
        }
        StringBuilder builder = new StringBuilder(result.size());
        for (int i = result.size() - 1; i >= 0; i-- )
        {
            builder.append(result.get(i));
        }
        return builder.toString();
    }
}


Multiply Strings

标签:style   color   io   os   ar   java   for   sp   div   

原文地址:http://blog.csdn.net/jiewuyou/article/details/39928173

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!