Skip to content

Commit dbf22c7

Browse files
committed
cleanup cpp files
1 parent 6448b80 commit dbf22c7

File tree

9 files changed

+139
-95
lines changed

9 files changed

+139
-95
lines changed

cpp/0001_two_sum/0001_two_sum.cc

Lines changed: 0 additions & 23 deletions
This file was deleted.

cpp/0001_two_sum/0001_two_sum.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
#include <unordered_map>
12
#include <vector>
23

4+
using std::unordered_map;
35
using std::vector;
46

57
class Solution {
68
public:
7-
vector<int> twoSum(vector<int> &nums, int target);
9+
vector<int> twoSum(vector<int> &nums, int target) {
10+
11+
unordered_map<int, int> umap;
12+
for (int i = 0; i < nums.size(); i++) {
13+
const int current = nums[i];
14+
15+
if (umap.count(current) > 0) {
16+
return {i, umap[current]};
17+
}
18+
19+
const int diff = target - nums[i];
20+
umap[diff] = i;
21+
}
22+
23+
return {};
24+
}
825
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <algorithm>
2+
#include <string>
3+
#include <unordered_map>
4+
5+
using ::std::max;
6+
using ::std::string;
7+
using ::std::unordered_map;
8+
9+
class Solution {
10+
public:
11+
int lengthOfLongestSubstring(string s) {
12+
unordered_map<char, int> hash;
13+
int length = 0;
14+
int max_length = 0;
15+
int start = 0;
16+
17+
for (int i = 0; i < s.length(); i++) {
18+
const char &letter = s[i];
19+
20+
if (hash.find(letter) != hash.end() && hash[letter] >= start) {
21+
start = hash[letter] + 1;
22+
length = i - start;
23+
}
24+
25+
length++;
26+
hash[letter] = i;
27+
max_length = max(length, max_length);
28+
}
29+
30+
return max_length;
31+
}
32+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "0003_longest_substring_without_repeating_characters.h"
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
5+
using ::std::string;
6+
7+
TEST(LengthOfLongestSubstring, Example1) {
8+
string s = "abcabcbb";
9+
10+
EXPECT_EQ(Solution().lengthOfLongestSubstring(s), 3);
11+
}
12+
13+
TEST(LengthOfLongestSubstring, Example2) {
14+
string s = "bbbbb";
15+
16+
EXPECT_EQ(Solution().lengthOfLongestSubstring(s), 1);
17+
}
18+
19+
TEST(LengthOfLongestSubstring, Example3) {
20+
string s = "pwwkew";
21+
22+
EXPECT_EQ(Solution().lengthOfLongestSubstring(s), 3);
23+
}
24+
25+
TEST(LengthOfLongestSubstring, Example4) {
26+
string s = "";
27+
28+
EXPECT_EQ(Solution().lengthOfLongestSubstring(s), 0);
29+
}

cpp/0020_valid_parentheses/0020_valid_parentheses.cc

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
#include <stdio.h>
1+
#include <stack>
22
#include <string>
3+
#include <unordered_map>
4+
5+
using std::stack;
6+
using std::unordered_map;
37

48
using std::string;
59

610
class Solution {
711
public:
8-
bool isValid(string s);
12+
bool isValid(string s) {
13+
unordered_map<char, char> dict({{'(', ')'}, {'[', ']'}, {'{', '}'}});
14+
stack<char> stack;
15+
16+
for (const char &letter : s) {
17+
if (dict.find(letter) != dict.end()) {
18+
stack.push(dict[letter]);
19+
continue;
20+
}
21+
22+
if (stack.empty()) {
23+
return false;
24+
}
25+
26+
const char &popped = stack.top();
27+
28+
if (letter != popped) {
29+
return false;
30+
}
31+
32+
stack.pop();
33+
}
34+
35+
return stack.empty();
36+
}
937
};

cpp/0200_number_of_islands/0200_number_of_islands.cc

Lines changed: 0 additions & 32 deletions
This file was deleted.

cpp/0200_number_of_islands/0200_number_of_islands.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,33 @@
33
using std::vector;
44

55
class Solution {
6+
7+
private:
8+
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
9+
if (row >= grid.size() || column >= grid[row].size() ||
10+
grid[row][column] == '0') {
11+
return;
12+
}
13+
14+
grid[row][column] = '0';
15+
dfs(grid, row + 1, column);
16+
dfs(grid, row, column + 1);
17+
dfs(grid, row - 1, column);
18+
dfs(grid, row, column - 1);
19+
}
20+
621
public:
7-
int numIslands(vector<vector<char>> &grid);
8-
};
22+
int numIslands(vector<vector<char>> &grid) {
23+
int islands = 0;
24+
for (int row = 0; row < grid.size(); row++) {
25+
for (int column = 0; column < grid[row].size(); column++) {
26+
if (grid[row][column] == '1') {
27+
islands++;
28+
dfs(grid, row, column);
29+
}
30+
}
31+
}
32+
33+
return islands;
34+
}
35+
};

cpp/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cc_library(
66
hdrs=glob(["**/**/*.h"]),
77
srcs = glob(
88
["**/**/*.cc"],
9-
exclude = ["**/**/*_test.cc"]
9+
exclude = ["**/**/*test.cc"]
1010
),
1111
visibility = ["//visibility:public"],
1212
)

0 commit comments

Comments
 (0)