zoukankan      html  css  js  c++  java
  • Multiply Strings

    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.

    思路:

      手工模拟乘法

    我的代码:

    public class Solution {
        public String multiply(String num1, String num2) {
            if(num1==null || num1.length()==0 || num2==null || num2.length()==0 || num1.charAt(0)=='0' || num2.charAt(0)=='0')    return "0";
            if(num1.charAt(0) == '0') num1 = num1.substring(1);
            if(num2.charAt(0) == '0') num2 = num2.substring(1);
            int one = num1.length();
            int two = num2.length();
            String[] rst = new String[one];
            
            for(int i=one-1; i>=0; i--)
            {
                StringBuffer sb = new StringBuffer();
                for(int j=one-1; j>i; j--) sb.append('0');
                int a = (int)(num1.charAt(i)-'0');
                int carry = 0;
                for(int j=two-1; j>=0; j--)
                {
                    int b = (int)(num2.charAt(j)-'0');
                    int c = a*b + carry;
                    sb.append(c%10);
                    carry = c/10;
                }
                if(carry != 0)  sb.append(carry);
                rst[i] = sb.toString();
            }
            
            int maxLength = Integer.MIN_VALUE;
            for(String s: rst)  maxLength = Math.max(maxLength, s.length());
            StringBuffer sb = new StringBuffer();
            int carry = 0;
            for(int i=0; i<maxLength; i++)
            {
                int sum = carry;
                for(String s: rst)
                {
                    sum += (s.length()<=i?0:(int)(s.charAt(i)-'0'));
                }
                sb.append(sum%10);
                carry = sum/10;
            }
            if(carry != 0) sb.append(carry);
            return sb.reverse().toString();
            
        }
        
    }
    View Code

    他人代码:

    public class Solution {
        public String multiply(String num1, String num2) {
            if (num1 == null || num2 == null) {
                return null;
            }
            
            int len1 = num1.length(), len2 = num2.length();
            int len3 = len1 + len2;
            int i, j, product, carry;
    
            int[] num3 = new int[len3];
            for (i = len1 - 1; i >= 0; i--) {
                carry = 0;
                for (j = len2 - 1; j >= 0; j--) {
                    product = carry + num3[i + j + 1] +
                        Character.getNumericValue(num1.charAt(i)) *
                        Character.getNumericValue(num2.charAt(j));
                    num3[i + j + 1] = product % 10;
                    carry = product / 10;
                }
                num3[i + j + 1] = carry;
            }
    
            StringBuilder sb = new StringBuilder();
            i = 0;
    
            while (i < len3 - 1 && num3[i] == 0) {
                i++;
            }
    
            while (i < len3) {
                sb.append(num3[i++]);
            }
    
            return sb.toString();
        }
    }
    View Code

    学习之处:

    • 记住一个结论:两个数相乘,位数不会大于二者的位数之和,所以这样可以提前设定结果数组的大小了,不用再像我用了string的数组去存储中间结果,浪费空间,浪费时间
    • 尽量少的用a,b等命名,用product多大气啊,另外能直接运算的就直接运算把,别再用变量进行记录传递了,关键是没作用,看起来代码啰嗦。
  • 相关阅读:
    2030年的10大热门职业
    10种散发着爱情信号的肢体语言
    猎头不来找你的5种原因
    15个不得不去的“秘密”景点
    保持微笑的五大好处
    vscode 格式化突然失效
    openlayers之全屏控件的使用
    openlayers之点,线,面(以城市,河流,省份为例,分别对应点线面)
    openlayers 添加标记点击弹窗 定位图标闪烁
    搜索框focus 搜索面板显示 点击别处消失 从浏览器别的页面回来消失
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4485184.html
Copyright © 2011-2022 走看看