mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
ok i'm back: 210-course-schedule-ii, 43-multiply-strings
This commit is contained in:
9
43-multiply-strings/driver.cpp
Normal file
9
43-multiply-strings/driver.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
string num1 = "123", num2 = "456", answer = "56088";
|
||||
Solution soln;
|
||||
cout << "Multiplied strings correctly? " << soln.test(num1, num2, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
33
43-multiply-strings/soln.cpp
Normal file
33
43-multiply-strings/soln.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
string Solution::multiply(string num1, string num2)
|
||||
{
|
||||
// product can't have more digits than sum of individual num's digits
|
||||
string prod(num1.size() + num2.size(), '0');
|
||||
int carry, tmp;
|
||||
|
||||
// standard multiplication
|
||||
// one digit of num1 with all of num2, accumulate
|
||||
for (int i = num1.size() - 1; i >= 0; i--)
|
||||
{
|
||||
carry = 0;
|
||||
for (int j = num2.size() - 1; j >= 0; j--)
|
||||
{
|
||||
tmp = (num1[i] - '0') * (num2[j] - '0') + (prod[i + j + 1] - '0') + carry;
|
||||
prod[i + j + 1] = (tmp % 10) + '0';
|
||||
carry = tmp / 10;
|
||||
}
|
||||
prod[i] += carry;
|
||||
}
|
||||
|
||||
// strip leading zeroes
|
||||
int start_pos = prod.find_first_not_of("0");
|
||||
if (start_pos != -1)
|
||||
return prod.substr(start_pos);
|
||||
return "0";
|
||||
}
|
||||
|
||||
bool Solution::test(string num1, string num2, string answer)
|
||||
{
|
||||
return multiply(num1, num2) == answer;
|
||||
}
|
||||
8
43-multiply-strings/soln.hpp
Normal file
8
43-multiply-strings/soln.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
string multiply(string num1, string num2);
|
||||
bool test(string num1, string num2, string answer);
|
||||
};
|
||||
Reference in New Issue
Block a user