File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Leetcode_Practice_Questions Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ // we will be writing the code for Multiply strings Leetcode# 43
2+ #include < iostream>
3+ #include < vector>
4+ #include < string>
5+ using namespace std ;
6+ string multiply (string num1, string num2) {
7+ if (num1 == " 0" || num2 == " 0" ) {
8+ return " 0" ;
9+ }
10+ vector<int > nums (num1.size () + num2.size (), 0 );
11+ for (int i = num1.size () - 1 ; i >= 0 ; i--) {
12+ for (int j = num2.size () - 1 ; j >= 0 ; j--) {
13+ int mul = (num1[i] - ' 0' ) * (num2[j] - ' 0' );
14+ int sum = mul + nums[i + j + 1 ];
15+ nums[i + j + 1 ] = sum % 10 ;
16+ nums[i + j] += sum / 10 ;
17+ }
18+ }
19+ string result = " " ;
20+ int i = 0 ;
21+ while (i < nums.size () && nums[i] == 0 ) {
22+ i++;
23+ }
24+ while (i < nums.size ()) {
25+ result.push_back (nums[i] + ' 0' );
26+ i++;
27+ }
28+ return result;
29+ }
30+ int main () {
31+ string num1 = " 123" ;
32+ string num2 = " 456" ;
33+ string answer = multiply (num1, num2);
34+ cout << " The string multiplied by " << num1 << " and " << num2 << " is " << answer << endl;
35+ return 0 ;
36+ }
You can’t perform that action at this time.
0 commit comments