zoukankan      html  css  js  c++  java
  • 43. Multiply Strings

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

    Example 1:

    Input: num1 = "2", num2 = "3"
    Output: "6"

    Example 2:

    Input: num1 = "123", num2 = "456"
    Output: "56088"
    

    Note:

    1. The length of both num1 and num2 is < 110.
    2. Both num1 and num2 contain only digits 0-9.
    3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
    4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

    my code:

    class Solution {
    public:
        string multiply(string num1, string num2) {
            int len1 = num1.length();
            int len2 = num2.length();
            if (len1 == 1 && num1[0] == '0' ||
                len2 == 1 && num2[0] == '0')
                return "0";
            vector<string> v;
            int incidental;
            int multiply;
            for (int i = len2 - 1; i >= 0; --i) {
                incidental = 0;
                int n2 = num2[i] - '0';
                string tem = "";
                for (int k = (len2 - 1) - i; k > 0; --k) {
                    tem += '0';
                }
                for (int j = len1 - 1; j >= 0; --j) {
                    int n1 = num1[j] - '0';
                    multiply = n1 * n2 + incidental;
                    incidental = multiply / 10;
                    tem += to_string(multiply%10);
                }
                if (incidental != 0) 
                    tem += to_string(incidental);
                v.push_back(tem);
            }
            int max_length = v.back().size();
            string ans = "";
            int flag;
            incidental = 0;
            for (int i = 0; i < max_length; ++i) {
                flag = 0;
                for (int j = 0; j < v.size(); ++j) {
                    if (i < v[j].size()) {
                        int tage = v[j][i] - '0';
                        flag += tage;
                    }
                }
                flag += incidental;
                
                incidental = flag / 10;
                ans = to_string(flag % 10) + ans;
            }
            if (incidental != 0) 
                ans = to_string(incidental) + ans;
            return ans;
        }
    };
    

    Runtime: 24 ms, faster than 14.44% of C++ online submissions for Multiply Strings.

    efficient code:

    class Solution {
    public:
        string multiply(string num1, string num2) {
            string sum(num1.size() + num2.size(), '0');
    
            for (int i = num1.size() - 1; 0 <= i; --i) {
                int carry = 0;
                for (int j = num2.size() - 1; 0 <= j; --j) {
                    int tmp = (sum[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry;
                    sum[i + j + 1] = tmp % 10 + '0';
                    carry = tmp / 10;
                }
                sum[i] += carry;
            }
    
            size_t startpos = sum.find_first_not_of("0");
            if (string::npos != startpos) {
                return sum.substr(startpos);
            }
            return "0";
        }
    };
    

    Runtime: 4 ms, faster than 100.00% of C++ online submissions for Multiply Strings.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    hibernate---核心开发接口1(重点)
    hibernate--联合主键(了解+,掌握-)
    hibernate---table_Generator
    hibernate---ID生成策略
    hibernate 注解写在哪?
    hibernate学习笔记--可选的配置属性
    软件开发的硬约束【转载】
    (2016春) 作业7: 用户体验设计案例分析
    (2016春) 第一阶段优胜者
    (2016春) 作业6 :团队作业
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9800318.html
Copyright © 2011-2022 走看看