[LeetCode]--43. 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. Converting the input string t

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.
Converting the input string to integer is NOT allowed.
You should NOT use internal library such as BigInteger.

字符串拆分成字符,然后计算,注意进位就行,进位中又要特别注意最高位进位。最开始我用int计算,其实还是逃不过超出范围的限制,所以就用个int的数组,只记录每一位数字,然后用这个数组来构造字符串。

public class Solution {
    public String multiply(String num1, String num2) {
        int[] res = new int[num1.length() + num2.length()];
        int temp = 0, flag = 0, i = num1.length() - 1, j = 0;
        for (; i >= 0; i--) {
            for (j = num2.length() - 1; j >= 0; j--) {
                temp = res[i + j + 1] + (num1.charAt(i) - '0')
                        * (num2.charAt(j) - '0') + flag;
                flag = temp / 10;
                res[i + j + 1] = temp % 10;
            }
            if (flag != 0) {
                res[i + j + 1] = flag;
                flag = 0;
            }
        }
        StringBuilder sb = new StringBuilder();
        i = 0;

        while (i < res.length - 1 && res[i] == 0) {
            i++;
        }

        while (i < res.length) {
            sb.append(res[i++]);
        }
        return sb.toString();
    }
}

还有一个比我简便一点点方法。

public String multiply2(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();
    }
目录
相关文章
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
108 0
LeetCode 415. Add Strings
LeetCode 205. Isomorphic Strings
给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
90 0
LeetCode 205. Isomorphic Strings
LeetCode 43. Multiply Strings
给定两个表示为字符串形式的非负整数num1和num2,返回num1和num2的乘积,也表示为字符串形式。
105 0
LeetCode 43. Multiply Strings
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
Java 索引 Python
LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
910 0
leetcode 205. Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic.
709 0
LeetCode - 43. Multiply Strings
43. Multiply Strings  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定两个字符串,计算这两个字符串相乘的结果.
939 0
|
索引 Java
LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611168 翻译 给定两个字符串s和t,决定它们是否是同构的。
861 0