Skip to content

Commit 2ea6155

Browse files
committed
update script
1 parent 3f83bff commit 2ea6155

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

compile_flags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-std=c++17
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <memory>
3+
4+
struct ListNode {
5+
int val;
6+
ListNode *next;
7+
ListNode() : val(0), next(nullptr) {}
8+
ListNode(int x) : val(x), next(nullptr) {}
9+
ListNode(int x, ListNode *next) : val(x), next(next) {}
10+
};
11+
12+
class Solution {
13+
public:
14+
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
15+
ListNode *l3 = new ListNode();
16+
ListNode *head = l3;
17+
18+
while (l1 && l2) {
19+
if (l1->val <= l2->val) {
20+
l3->next = l1;
21+
l1 = l1->next;
22+
} else {
23+
l3->next = l2;
24+
l2 = l2->next;
25+
}
26+
27+
l3 = l3->next;
28+
}
29+
30+
if (l1 != nullptr) {
31+
l3->next = l1;
32+
}
33+
34+
if (l2 != nullptr) {
35+
l3->next = l2;
36+
}
37+
38+
return head->next;
39+
}
40+
};

cpp/0021_merge_two_sorted_lists/0021_merge_two_sorted_lists_test.cc

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Given two non-negative integers num1 and num2 represented as strings,
2+
* return the product of num1 and num2, also represented as a string.
3+
*/
4+
5+
#include <string>
6+
#include <math.h>
7+
#include <vector>
8+
#include <algorithm>
9+
10+
using std::string;
11+
12+
class Solution {
13+
public:
14+
string multiply(string num1, string num2) {
15+
std::vector<int> result(num1.length() + num2.length(), 0);
16+
17+
int flag = 0;
18+
19+
for (int index1 = num1.length() - 1; index1 >= 0; index1--) {
20+
int carry = 0;
21+
int result_index = 0;
22+
for (int index2 = num2.length() - 1; index2 >= 0; index2--) {
23+
const int& raw_result = num1[index1] * num2[index2] + carry + result[result_index + flag];
24+
result[result_index +flag] = raw_result % 10;
25+
carry = floor(raw_result / 10);
26+
result_index++;
27+
}
28+
flag++;
29+
}
30+
31+
while(result.size() > 1) {
32+
if (result.back() > 0) {
33+
break;
34+
}
35+
result.pop_back();
36+
}
37+
38+
std::reverse(result.begin(), result.end());
39+
40+
string string_result = "";
41+
42+
for(const int& letter: result) {
43+
string_result += std::to_string(letter);
44+
}
45+
46+
return string_result;
47+
}
48+
};
49+
50+
/*
51+
32
52+
219
53+
---
54+
carry = 0
55+
56+
[8, 0, 0, 7, 0]
57+
58+
7008
59+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { multiply } from "./0043_multiply_strings";
2+
3+
test("multiply1", () => {
4+
expect(multiply("2", "3")).toEqual("6");
5+
});
6+
7+
test("multiply2", () => {
8+
expect(multiply("589", "24")).toBe("14136");
9+
});
10+
11+
test("multiply3", () => {
12+
expect(multiply("5891", "243")).toBe("1431513");
13+
});
14+
15+
test("multiply4", () => {
16+
expect(multiply("123", "456")).toEqual("56088");
17+
});
18+
19+
test("multiply5", () => {
20+
expect(multiply("0", "0")).toEqual("0");
21+
});

0 commit comments

Comments
 (0)