leetcode刷题42

今天刷的题是LeetCode43题,这个题是给定两个字符串,求乘积

具体地,我的代码就是根据乘法思路来做的,末位补零来解决高低位的问题

public class Multiply_43_middle {
    public static void main(String[] args) {
        System.out.println(solution("9133","0"));
    }
    public static String solution(String num1,String num2){
        int n=num1.length();
        StringBuilder stringBuilder=new StringBuilder();//保存的是结果
        for (int i = n-1; i >=0 ; i--) {
            int multiplier=Integer.parseInt(String.valueOf(num1.charAt(i)));
            int j=num2.length()-1;
            int pre=0;//保存需要进位的数
            StringBuilder stringBuilder1=new StringBuilder();
            for (int k = 0; k <n-1-i ; k++) {
                stringBuilder1.append(0);
            }
            while (j>=0){
                int antherNum=Integer.parseInt(String.valueOf(num2.charAt(j)));//另一个乘数
                int product=pre+antherNum*multiplier;//计算两个数的乘积
                pre=product/10;//计算进位的数
                product=product%10;//除去进位的数之后,个位数
                stringBuilder1.insert(0,product);
                j--;
            }
            if (pre!=0){
                stringBuilder1.insert(0,pre);
            }
            //接下来需要把stringbuilder1添加到stringbuilder中
            stringBuilder=add(stringBuilder,stringBuilder1);
        }
        if (Integer.parseInt(String.valueOf(stringBuilder.toString().charAt(0)))==0)return "0";
        else return stringBuilder.toString();
    }
    public static StringBuilder add(StringBuilder string1,StringBuilder string2){
        int pre=0;
        StringBuilder result=new StringBuilder();
        for (int i = string2.length()-1; i >=0 ; i--) {
            if (string2.length()==string1.length()){
                int num1=Integer.parseInt(String.valueOf(string1.toString().charAt(i)));
                int num2=Integer.parseInt(String.valueOf(string2.toString().charAt(i)));
                int sum=num1+num2+pre;
                pre=sum/10;
                result.insert(0, String.valueOf(sum%10));
            }else if (string2.length()<string1.length()){
                int difference=string1.length()-string2.length();
                int num1=Integer.parseInt(String.valueOf(string1.toString().charAt(i+difference)));
                int num2=Integer.parseInt(String.valueOf(string2.toString().charAt(i)));
                int sum=num1+num2+pre;
                pre=sum/10;
                result.insert(0, String.valueOf(sum%10));
            }else {
                //string2.length>string1.length
                int difference=string2.length()-string1.length();
                int num1=0;
                if (i-difference>=0){
                    num1=Integer.parseInt(String.valueOf(string1.toString().charAt(i-difference)));
                }
                int num2=Integer.parseInt(String.valueOf(string2.toString().charAt(i)));
                int sum=num1+num2+pre;
                pre=sum/10;
                result.insert(0,String.valueOf(sum%10));
            }
        }
        if (pre!=0)result.insert(0,pre);
        return result;
    }
}

 

posted @ 2019-09-28 16:22  刘云生  阅读(145)  评论(0编辑  收藏  举报