aboutsummaryrefslogtreecommitdiff
path: root/multiply_strings.cpp
blob: 115b85cceed4768c7164482ce6460df850e009ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "leetcode.h"

class Solution {
public:
  string multiply(string num1, string num2) {
    if (num1 == "0" || num2 == "0")
      return "0";
    vector<int> res(num1.size() + num2.size(), 0);
    for (int i = num1.size() - 1; i >= 0; i--) {
      for (int j = num2.size() - 1; j >= 0; j--) {
        res[i + j + 1] += (num1[i] - '0') * (num2[j] - '0');
        res[i + j] += res[i + j + 1] / 10;
        res[i + j + 1] %= 10;
      }
    }
    int i = 0;
    string ans = "";
    while (res[i] == 0)
      i++;
    while (i < res.size())
      ans += to_string(res[i++]);
    return ans;
  }
};

int main() {}