【java】43. Multiply Strings

问题原文https://leetcode-cn.com/problems/multiply-strings/description/

     //任意一个整数和长度为1的整数相乘
    public String helpFun(String x,String y){
        StringBuilder res = new StringBuilder();
        int len = x.length();
        Stack<Integer> stack = new Stack<Integer>();
        for(int i = len-1;i >= 0;i--){
            int tmp = (x.charAt(i)-48)*(y.charAt(0)-48);
            if (!stack.isEmpty()) {
                tmp+=stack.pop();
            }
            if(tmp <= 9) {
                stack.add(tmp);
                stack.add(0);
            }
            else {
                stack.add(tmp % 10);
                stack.add(tmp / 10);
            }
        }
        if (!stack.isEmpty()&&stack.peek() == 0) stack.pop();
        while(!stack.isEmpty()){
            res.append(stack.pop());
        }
        return res.toString();
    }
    //任意两个整数相加
    public String plus(String x ,String y){
        StringBuilder res = new StringBuilder();
        Stack<Integer> stack = new Stack<Integer>();
        int len1 = x.length();
        int len2 = y.length();
        int i = 0;
        for (;i<len1&&i<len2;i++){
            int tmp = (x.charAt(len1-i-1)-48)+(y.charAt(len2-i-1)-48);
            if (!stack.isEmpty()) tmp+=stack.pop();
            if (tmp>9){
                stack.add(tmp%10);
                stack.add(tmp/10);
            }else{
                stack.add(tmp);
                stack.add(0);
            }
        }
        while(i < len1){
            int tmp = x.charAt(len1-i-1)-48;
            if (!stack.isEmpty()) tmp+=stack.pop();
            if (tmp>9){
                stack.add(tmp%10);
                stack.add(tmp/10);
            }else{
                stack.add(tmp);
                stack.add(0);
            }
            i++;
        }
        while(i < len2){
            int tmp = y.charAt(len2-i-1)-48;
            if (!stack.isEmpty()) tmp+=stack.pop();
            if (tmp>9){
                stack.add(tmp%10);
                stack.add(tmp/10);
            }else{
                stack.add(tmp);
                stack.add(0);
            }
            i++;
        }
        if (stack.peek() == 0) stack.pop();
        while(!stack.isEmpty()){
            res.append(stack.pop());
        }
        return res.toString();
    }
    public String multiply(String num1, String num2) {
        if(num1.equals("0") || num2.equals("0")) return "0";
        int len = num2.length();
        String res  = "0";
        for (int i = len-1;i >= 0;i--){
            String tmp = helpFun(num1,String.valueOf(num2.charAt(i)));
            for (int j = 0;j<len-1-i;j++)
                tmp+="0";
            res = plus(res,tmp);
        }
        return res;
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值