LeetCode 110 Multiply Strings

来源:互联网 发布:2011选秀动态体测数据 编辑:程序博客网 时间:2024/06/06 08:38
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.

分析:

这属于用字符串来模拟数学运算的题目。

明确几点:

1,两个整数相乘,结果的位数最多为两数位数之和,比如99*99;

2,反向保存数字有助于位数的计算。

public class Solution {    public String multiply(String num1, String num2) {        //两数反向,String没有reverse()方法,所以借助于StringBuffer        num1 = new StringBuffer(num1).reverse().toString();        num2 = new StringBuffer(num2).reverse().toString();        //相乘结果最长是num1.length()+num2.length(), 例如,99*99        int[] res = new int[num1.length()+num2.length()];        for(int i=0; i<num1.length(); i++){            int a = num1.charAt(i)-'0';            for(int j=0; j<num2.length(); j++){                int b = num2.charAt(j)-'0';                res[i+j] = res[i+j] + a*b;//先把结果存在相应位置再考虑进位            }        }        StringBuffer sb = new StringBuffer();        for(int i=0; i<res.length; i++){            int digit = res[i]%10;            int carry = res[i]/10;            //从头插入            sb.insert(0,digit);            if(i < res.length-1)                res[i+1] = res[i+1]+carry;        }        while(sb.length()>0 && sb.charAt(0)=='0')            sb.deleteCharAt(0);        return sb.length()==0 ? "0" : sb.toString();    }}


0 0
原创粉丝点击