Skip to content

Commit 0dd18f4

Browse files
committed
fix multiply exercise
1 parent 18417a3 commit 0dd18f4

File tree

8 files changed

+90
-49
lines changed

8 files changed

+90
-49
lines changed

cpp/0001_two_sum/0001_two_sum.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class Solution {
88
public:
99
vector<int> twoSum(vector<int> &nums, int target) {
1010
unordered_map<int, int> umap;
11-
for (int i = 0; i < nums.size(); i++) {
11+
for (size_t i = 0; i < nums.size(); i++) {
1212
const int current = nums[i];
1313

1414
if (umap.count(current) > 0) {
15-
return {i, umap[current]};
15+
return {(int)i, umap[current]};
1616
}
1717

1818
const int diff = target - nums[i];

cpp/0003_longest_substring_without_repeating_characters/0003_longest_substring_without_repeating_characters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Solution {
1414
int max_length = 0;
1515
int start = 0;
1616

17-
for (int i = 0; i < s.length(); i++) {
17+
for (size_t i = 0; i < s.length(); i++) {
1818
const char &letter = s[i];
1919

2020
if (hash.find(letter) != hash.end() && hash[letter] >= start) {

cpp/0020_valid_parentheses/0020_valid_parentheses_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ using std::string;
77
TEST(ValidParenthesisTest, SimpleString) {
88
string s = "()";
99

10-
ASSERT_TRUE(Solution().isValid(s));
10+
EXPECT_TRUE(Solution().isValid(s));
1111
}
1212

1313
TEST(ValidParenthesisTest, AllSymbolsString) {
1414
string s = "()[]{}";
1515

16-
ASSERT_TRUE(Solution().isValid(s));
16+
EXPECT_TRUE(Solution().isValid(s));
1717
}
1818

1919
TEST(ValidParenthesisTest, InvalidString) {
2020
string s = "(]";
2121

22-
ASSERT_FALSE(Solution().isValid(s));
22+
EXPECT_FALSE(Solution().isValid(s));
2323
}
2424

2525
TEST(ValidParenthesisTest, InvalidString2) {
2626
string s = "]";
2727

28-
ASSERT_FALSE(Solution().isValid(s));
28+
EXPECT_FALSE(Solution().isValid(s));
2929
}

cpp/0043_multiply_strings/0043_multiply_strings.h

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,56 @@
22
* return the product of num1 and num2, also represented as a string.
33
*/
44

5-
#include <string>
5+
#include <algorithm>
66
#include <math.h>
7+
#include <string>
78
#include <vector>
8-
#include <algorithm>
99

1010
using std::string;
1111

1212
class Solution {
1313
public:
1414
string multiply(string num1, string num2) {
15-
std::vector<int> result(num1.length() + num2.length(), 0);
15+
if (num1.length() < num2.length()) {
16+
std::swap(num1, num2);
17+
}
1618

19+
std::vector<int> result(num1.length() + num2.length(), 0);
20+
int carry = 0;
1721
int flag = 0;
1822

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++;
23+
for (int i = num2.length() - 1; i >= 0; i--) {
24+
for (int j = num1.length() - 1; j >= 0; j--) {
25+
const int result_index = num1.length() - 1 - j + flag;
26+
const int a = num1[j] - '0';
27+
const int b = num2[i] - '0';
28+
const int current = a * b + carry + result[result_index];
29+
carry = floor(current / 10);
30+
result[result_index] = current % 10;
31+
if (j == 0 && carry > 0) {
32+
result[num1.length() + flag] += carry;
33+
carry = 0;
34+
}
2735
}
2836
flag++;
2937
}
3038

31-
while(result.size() > 1) {
39+
while (result.size() > 1) {
3240
if (result.back() > 0) {
3341
break;
3442
}
43+
3544
result.pop_back();
3645
}
3746

3847
std::reverse(result.begin(), result.end());
3948

4049
string string_result = "";
4150

42-
for(const int& letter: result) {
51+
for (const int &letter : result) {
4352
string_result += std::to_string(letter);
4453
}
4554

4655
return string_result;
4756
}
4857
};
49-
50-
/*
51-
32
52-
219
53-
---
54-
carry = 0
55-
56-
[8, 0, 0, 7, 0]
57-
58-
7008
59-
*/
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import { multiply } from "./0043_multiply_strings";
1+
#include "0043_multiply_strings.h"
2+
#include <gtest/gtest.h>
23

3-
test("multiply1", () => {
4-
expect(multiply("2", "3")).toEqual("6");
5-
});
4+
TEST(MultiplyStrings, Example1) {
5+
EXPECT_EQ(Solution().multiply("2", "3"), "6");
6+
}
67

7-
test("multiply2", () => {
8-
expect(multiply("589", "24")).toBe("14136");
9-
});
8+
TEST(MultiplyStrings, Example2) {
9+
EXPECT_EQ(Solution().multiply("589", "24"), "14136");
10+
}
1011

11-
test("multiply3", () => {
12-
expect(multiply("5891", "243")).toBe("1431513");
13-
});
12+
TEST(MultiplyStrings, Example3) {
13+
EXPECT_EQ(Solution().multiply("5891", "243"), "1431513");
14+
}
1415

15-
test("multiply4", () => {
16-
expect(multiply("123", "456")).toEqual("56088");
17-
});
16+
TEST(MultiplyStrings, Example4) {
17+
EXPECT_EQ(Solution().multiply("123", "456"), "56088");
18+
}
1819

19-
test("multiply5", () => {
20-
expect(multiply("0", "0")).toEqual("0");
21-
});
20+
TEST(MultiplyStrings, Example5) {
21+
EXPECT_EQ(Solution().multiply("0", "0"), "0");
22+
}

cpp/0200_number_of_islands/0200_number_of_islands.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Solution {
2121
public:
2222
int numIslands(vector<vector<char>> &grid) {
2323
int islands = 0;
24-
for (int row = 0; row < grid.size(); row++) {
25-
for (int column = 0; column < grid[row].size(); column++) {
24+
for (size_t row = 0; row < grid.size(); row++) {
25+
for (size_t column = 0; column < grid[row].size(); column++) {
2626
if (grid[row][column] == '1') {
2727
islands++;
2828
dfs(grid, row, column);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <unordered_set>
2+
#include <vector>
3+
4+
using ::std::unordered_set;
5+
using ::std::vector;
6+
7+
class Solution {
8+
public:
9+
bool containsDuplicate(vector<int> &nums) {
10+
unordered_set<int> unique_numbers;
11+
for (const int &number : nums) {
12+
if (unique_numbers.find(number) != unique_numbers.end()) {
13+
return true;
14+
}
15+
unique_numbers.insert(number);
16+
}
17+
18+
return false;
19+
}
20+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "0217_contains_duplicate.h"
2+
#include <gtest/gtest.h>
3+
4+
using std::vector;
5+
6+
TEST(ContainsDuplicateTest, Example1) {
7+
vector<int> nums = {1, 2, 3, 1};
8+
9+
ASSERT_TRUE(Solution().containsDuplicate(nums));
10+
}
11+
12+
TEST(ContainsDuplicateTest, Example2) {
13+
vector<int> nums = {1, 2, 3, 4};
14+
15+
ASSERT_FALSE(Solution().containsDuplicate(nums));
16+
}
17+
18+
TEST(ContainsDuplicateTest, Example3) {
19+
vector<int> nums = {1, 1, 1, 3, 3, 4, 3, 2, 4, 2};
20+
21+
ASSERT_TRUE(Solution().containsDuplicate(nums));
22+
}

0 commit comments

Comments
 (0)