|
| 1 | +class Solution { |
| 2 | +private: |
| 3 | + // Calculate the sum of all of the results from multiplyOneDigit. |
| 4 | + string sumResults(vector<vector<int>>& results) { |
| 5 | + // Initialize answer as a number from results. |
| 6 | + vector<int> answer = results.back(); |
| 7 | + vector<int> newAnswer; |
| 8 | + results.pop_back(); |
| 9 | + |
| 10 | + // Sum each digit from answer and result |
| 11 | + for (vector<int> result : results) { |
| 12 | + newAnswer.clear(); |
| 13 | + int carry = 0; |
| 14 | + |
| 15 | + for (int i = 0; i < answer.size() || i < result.size(); ++i) { |
| 16 | + // If answer is shorter than result or vice versa, use 0 as the current digit. |
| 17 | + int digit1 = i < result.size() ? result[i] : 0; |
| 18 | + int digit2 = i < answer.size() ? answer[i] : 0; |
| 19 | + // Add current digits of both numbers. |
| 20 | + int sum = digit1 + digit2 + carry; |
| 21 | + // Set carry equal to the tens place digit of sum. |
| 22 | + carry = sum / 10; |
| 23 | + // Append the ones place digit of sum to answer. |
| 24 | + newAnswer.push_back(sum % 10); |
| 25 | + } |
| 26 | + |
| 27 | + if (carry) { |
| 28 | + newAnswer.push_back(carry); |
| 29 | + } |
| 30 | + answer = newAnswer; |
| 31 | + } |
| 32 | + |
| 33 | + // Convert answer to a string. |
| 34 | + string finalAnswer; |
| 35 | + for (int digit : answer) { |
| 36 | + finalAnswer.push_back(digit + '0'); |
| 37 | + } |
| 38 | + return finalAnswer; |
| 39 | + } |
| 40 | + |
| 41 | + // Multiply the current digit of secondNumber with firstNumber. |
| 42 | + vector<int> multiplyOneDigit(string& firstNumber, char& secondNumberDigit, int numZeros) { |
| 43 | + // Insert zeros at the beginning based on the current digit's place. |
| 44 | + vector<int> currentResult(numZeros, 0); |
| 45 | + int carry = 0; |
| 46 | + |
| 47 | + // Multiply firstNumber with the current digit of secondNumber. |
| 48 | + for (char firstNumberDigit : firstNumber) { |
| 49 | + int multiplication = (secondNumberDigit - '0') * (firstNumberDigit - '0') + carry; |
| 50 | + // Set carry equal to the tens place digit of multiplication. |
| 51 | + carry = multiplication / 10; |
| 52 | + // Append last digit to the current result. |
| 53 | + currentResult.push_back(multiplication % 10); |
| 54 | + } |
| 55 | + |
| 56 | + if (carry) { |
| 57 | + currentResult.push_back(carry); |
| 58 | + } |
| 59 | + return currentResult; |
| 60 | + } |
| 61 | + |
| 62 | +public: |
| 63 | + string multiply(string firstNumber, string secondNumber) { |
| 64 | + if (firstNumber == "0" || secondNumber == "0") { |
| 65 | + return "0"; |
| 66 | + } |
| 67 | + |
| 68 | + // Reverse both numbers. |
| 69 | + reverse(firstNumber.begin(), firstNumber.end()); |
| 70 | + reverse(secondNumber.begin(), secondNumber.end()); |
| 71 | + |
| 72 | + // For each digit in secondNumber, multipy the digit by firstNumber and |
| 73 | + // store the multiplication result (reversed) in results. |
| 74 | + vector<vector<int>> results; |
| 75 | + for (int i = 0; i < secondNumber.size(); ++i) { |
| 76 | + results.push_back(multiplyOneDigit(firstNumber, secondNumber[i], i)); |
| 77 | + } |
| 78 | + |
| 79 | + // Add all the results in the results array, and store the sum in the answer string. |
| 80 | + string answer = sumResults(results); |
| 81 | + |
| 82 | + // answer is reversed, so reverse it to get the final answer. |
| 83 | + reverse(answer.begin(), answer.end()); |
| 84 | + return answer; |
| 85 | + } |
| 86 | +}; |
0 commit comments