Skip to content

Commit 416ec09

Browse files
author
Kun Luo
committed
format
1 parent 6dda937 commit 416ec09

File tree

66 files changed

+426
-545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+426
-545
lines changed

src/4sum.cc

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
using namespace std;
44

55
class Solution {
6-
public:
7-
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
6+
public:
7+
int fourSumCount(vector<int> &A, vector<int> &B, vector<int> &C, vector<int> &D) {
88
int counter = 0;
99
map<int, int> sum_map;
1010
for (const auto a : A) {
11-
for (const auto b : B) {
12-
++sum_map[a + b];
13-
}
11+
for (const auto b : B) { ++sum_map[a + b]; }
1412
}
1513
for (const auto c : C) {
16-
for (const auto d : D) {
17-
counter += sum_map[-(c + d)];
18-
}
14+
for (const auto d : D) { counter += sum_map[-(c + d)]; }
1915
}
2016
return counter;
2117
}
2218
};
2319

24-
int main(int argc, char const* argv[]) {
20+
int main(int argc, char const *argv[]) {
2521
Solution solution;
2622
vector<int> a{0, 1, -1};
2723
vector<int> b{-1, 1, 0};

src/add-two-numbers.cc

+7-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ using namespace std;
44
using namespace utils;
55

66
class Solution {
7-
public:
8-
ListNode* addTwoNumbers(ListNode* ln, ListNode* rn) {
7+
public:
8+
ListNode *addTwoNumbers(ListNode *ln, ListNode *rn) {
99
int32_t val = 0, flow = 0;
1010
ListNode *ret = nullptr, *cur = nullptr;
1111

@@ -42,19 +42,17 @@ class Solution {
4242
cur = cur->next;
4343
}
4444

45-
if (flow != 0) {
46-
cur->next = new ListNode{flow};
47-
}
45+
if (flow != 0) { cur->next = new ListNode{flow}; }
4846
return ret;
4947
}
5048
};
5149

52-
int main(int argc, char const* argv[]) {
53-
ListNode* a = make_nodes({2, 4, 3});
54-
ListNode* b = make_nodes({5, 6, 7, 2});
50+
int main(int argc, char const *argv[]) {
51+
ListNode *a = make_nodes({2, 4, 3});
52+
ListNode *b = make_nodes({5, 6, 7, 2});
5553

5654
Solution solution;
57-
ListNode* res = solution.addTwoNumbers(a, b);
55+
ListNode *res = solution.addTwoNumbers(a, b);
5856
print_nodes(res);
5957
del_nodes(a);
6058
del_nodes(b);

src/basic-calculator.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using namespace std;
44

55
class Solution {
6-
public:
7-
int calculate(const string& str) {
6+
public:
7+
int calculate(const string &str) {
88
size_t idx = 0;
99
return calculate(str, idx);
1010
}
1111

12-
int calculate(const string& str, size_t& idx) const {
12+
int calculate(const string &str, size_t &idx) const {
1313
int64_t res = 0;
1414
for (char opr = '+'; idx < str.size();) {
1515
switch (str[idx]) {
@@ -23,9 +23,7 @@ class Solution {
2323
default:
2424
if (isdigit(str[idx])) {
2525
int64_t x = str[idx++] - '0';
26-
while (idx < str.size() && isdigit(str[idx])) {
27-
x = x * 10 + str[idx++] - '0';
28-
}
26+
while (idx < str.size() && isdigit(str[idx])) { x = x * 10 + str[idx++] - '0'; }
2927
res += opr == '+' ? x : -x;
3028
} else {
3129
++idx;
@@ -37,7 +35,7 @@ class Solution {
3735
}
3836
};
3937

40-
int main(int argc, char const* argv[]) {
38+
int main(int argc, char const *argv[]) {
4139
Solution solution;
4240
cout << solution.calculate("1-(2-(3-2))") << endl; // 0
4341
cout << solution.calculate("1 + 1") << endl; // 2

src/best-time-to-buy-and-sell-stock-iii.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ using namespace std;
44
using namespace utils;
55

66
class Solution {
7-
public:
8-
int maxProfit(vector<int>& prices) {
7+
public:
8+
int maxProfit(vector<int> &prices) {
99
auto fst_buy = numeric_limits<int>::min(), fst_sell = 0;
1010
auto sec_buy = numeric_limits<int>::min(), sec_sell = 0;
11-
for (const auto& price : prices) {
11+
for (const auto &price : prices) {
1212
fst_buy = std::max(fst_buy, -price);
1313
fst_sell = std::max(fst_sell, fst_buy + price);
1414
sec_buy = std::max(sec_buy, fst_sell - price);
@@ -18,7 +18,7 @@ class Solution {
1818
}
1919
};
2020

21-
int main(int argc, char const* argv[]) {
21+
int main(int argc, char const *argv[]) {
2222
vector<int> vec{1, 2, 3};
2323
Solution solution;
2424
cout << solution.maxProfit(vec);

src/binary-tree-maximum-path-sum.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ using namespace std;
44
using namespace utils;
55

66
class Solution {
7-
public:
8-
int maxPathSum(TreeNode* root) {
7+
public:
8+
int maxPathSum(TreeNode *root) {
99
int res = std::numeric_limits<int>::min();
1010
find_max_sum(root, res);
1111
return res;
1212
}
1313

14-
int find_max_sum(TreeNode* root, int& res) const {
14+
int find_max_sum(TreeNode *root, int &res) const {
1515
if (root == nullptr) return 0;
1616
const int ls = max(0, find_max_sum(root->left, res));
1717
const int rs = max(0, find_max_sum(root->right, res));
@@ -20,7 +20,7 @@ class Solution {
2020
}
2121
};
2222

23-
int main(int argc, char const* argv[]) {
23+
int main(int argc, char const *argv[]) {
2424
Solution solution;
2525
const auto tree = make_tree(-10, make_tree(9), make_tree(20, make_tree(15), make_tree(17)));
2626
cout << solution.maxPathSum(tree) << endl;

src/broken-board-dominoes.cc

+9-15
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55

66
using namespace std;
77

8-
ostream& operator<<(ostream& os, const std::vector<vector<char>>& board) {
9-
for (auto&& row : board) {
10-
for (auto&& col : row) {
11-
os << col << " ";
12-
}
8+
ostream &operator<<(ostream &os, const std::vector<vector<char>> &board) {
9+
for (auto &&row : board) {
10+
for (auto &&col : row) { os << col << " "; }
1311
os << endl;
1412
}
1513
return os;
1614
}
1715

1816
class Solution {
19-
public:
20-
int domino(int n, int m, const vector<vector<int>>& broken) {
17+
public:
18+
int domino(int n, int m, const vector<vector<int>> &broken) {
2119
vector<vector<char>> board(n, vector<char>(m, 0));
22-
for (auto&& xy : broken) {
23-
board[xy[0]][xy[1]] = 'X';
24-
}
20+
for (auto &&xy : broken) { board[xy[0]][xy[1]] = 'X'; }
2521
const int L = (n * m - broken.size()) / 2;
2622
int res = 0;
2723
stack<tuple<char, int, int>> opr_stk;
@@ -51,12 +47,10 @@ class Solution {
5147
// 回退
5248
if (row >= n) {
5349
res = std::max<int>(res, opr_stk.size());
54-
if (opr_stk.size() == L) {
55-
return L;
56-
}
50+
if (opr_stk.size() == L) { return L; }
5751
for (;;) {
5852
if (opr_stk.empty()) return res;
59-
auto& [dir, x, y] = opr_stk.top();
53+
auto &[dir, x, y] = opr_stk.top();
6054
if (dir == '-') {
6155
board[x][y + 1] = 0;
6256
if (x + 1 < n && board[x + 1][y] == 0) {
@@ -82,7 +76,7 @@ class Solution {
8276
}
8377
};
8478

85-
int main(int argc, char const* argv[]) {
79+
int main(int argc, char const *argv[]) {
8680
Solution solution;
8781

8882
// cout << solution.domino(2, 3, {{1, 0}, {1, 1}}) << endl;

src/container-with-most-water.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using namespace std;
44

55
class Solution {
6-
public:
7-
int maxArea(const vector<int>& height) {
6+
public:
7+
int maxArea(const vector<int> &height) {
88
switch (height.size()) {
99
case 0:
1010
case 1: return 0;
@@ -20,16 +20,14 @@ class Solution {
2020
--r;
2121
}
2222
const auto area = h * (r - l + 1);
23-
if (area > max_area) {
24-
max_area = area;
25-
}
23+
if (area > max_area) { max_area = area; }
2624
}
2725
return max_area;
2826
}
2927
}
3028
};
3129

32-
int main(int argc, char const* argv[]) {
30+
int main(int argc, char const *argv[]) {
3331
vector<int> vec{1, 8, 6, 2, 5, 4, 8, 3, 7};
3432
Solution solution;
3533
std::cout << solution.maxArea(vec);

src/distinct-subsequences.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ using namespace std;
44
using namespace utils;
55

66
class Solution {
7-
public:
8-
static int num_distinct(const string& src, const string& dst) {
7+
public:
8+
static int num_distinct(const string &src, const string &dst) {
99
const size_t M = src.length(), N = dst.size();
1010
vector<vector<uint32_t>> dp(N + 1, vector<uint32_t>(M + 1));
1111
std::fill(dp[0].begin(), dp[0].end(), 1);
1212
for (size_t i = 1; i <= dst.size(); ++i) {
13-
if (dst[i - 1] == src[i - 1]) {
14-
dp[i][i] = dp[i - 1][i - 1];
15-
}
13+
if (dst[i - 1] == src[i - 1]) { dp[i][i] = dp[i - 1][i - 1]; }
1614
for (size_t j = i + 1; j <= src.size(); ++j) {
1715
if (dst[i - 1] == src[j - 1]) {
1816
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
@@ -25,7 +23,7 @@ class Solution {
2523
}
2624
};
2725

28-
int main(int argc, char const* argv[]) {
26+
int main(int argc, char const *argv[]) {
2927
cout << Solution::num_distinct("xslledayhxhadmctrliaxqpokyezcfhzaskeykchkmhpyjipxtsuljkwkovmvel"
3028
"vwxzwieeuqnjozrfwmzsylcwvsthnxujvrkszqwtglewkycikdaiocglwzukwov"
3129
"sghkhyidevhbgffoqkpabthmqihcfxxzdejletqjoxmwftlxfcxgxgvpperwbqv"

src/divisor-game.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
using namespace std;
55

66
class Solution {
7-
public:
8-
bool divisorGame(int N) { return !(N & 1); }
7+
public:
8+
bool divisorGame(int N) {
9+
return !(N & 1);
10+
}
911
};
1012

11-
int main(int argc, char const* argv[]) {
13+
int main(int argc, char const *argv[]) {
1214
Solution solution;
1315
cout << (solution.divisorGame(2) ? "True" : "False") << endl;
1416
}

src/dungeon-game.cc

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,21 @@
55
using namespace std;
66

77
class Solution {
8-
public:
9-
int calculateMinimumHP(const vector<vector<int>>& dungeon) {
8+
public:
9+
int calculateMinimumHP(const vector<vector<int>> &dungeon) {
1010
const int M = dungeon.size(), N = dungeon.front().size();
1111
vector<vector<int>> dp(M, vector<int>(N, 0));
1212
dp[M - 1][N - 1] = max(1, 1 - dungeon[M - 1][N - 1]);
13+
for (int i = M - 2; i >= 0; --i) { dp[i][N - 1] = max(1, dp[i + 1][N - 1] - dungeon[i][N - 1]); }
14+
for (int j = N - 2; j >= 0; --j) { dp[M - 1][j] = max(1, dp[M - 1][j + 1] - dungeon[M - 1][j]); }
1315
for (int i = M - 2; i >= 0; --i) {
14-
dp[i][N - 1] = max(1, dp[i + 1][N - 1] - dungeon[i][N - 1]);
15-
}
16-
for (int j = N - 2; j >= 0; --j) {
17-
dp[M - 1][j] = max(1, dp[M - 1][j + 1] - dungeon[M - 1][j]);
18-
}
19-
for (int i = M - 2; i >= 0; --i) {
20-
for (int j = N - 2; j >= 0; --j) {
21-
dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
22-
}
16+
for (int j = N - 2; j >= 0; --j) { dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]); }
2317
}
2418
return dp.front().front();
2519
}
2620
};
2721

28-
int main(int argc, char const* argv[]) {
22+
int main(int argc, char const *argv[]) {
2923
Solution solution;
3024
cout << solution.calculateMinimumHP({{-2, -3, 3}, {-5, -10, 1}, {10, 30, -5}}) << endl; // 7
3125
}

src/edit-distance.cc

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22

33
using namespace std;
44

5-
inline void print_dp(const vector<vector<int>>& dp) {
5+
inline void print_dp(const vector<vector<int>> &dp) {
66
system("cls");
7-
for (auto&& vec : dp) {
8-
for (auto&& val : vec) {
9-
cout << setw(4) << val;
10-
}
7+
for (auto &&vec : dp) {
8+
for (auto &&val : vec) { cout << setw(4) << val; }
119
cout << endl;
1210
}
1311
}
1412

1513
class Solution {
16-
public:
17-
int minDistance(const string& w1, const string& w2) {
14+
public:
15+
int minDistance(const string &w1, const string &w2) {
1816
const size_t M = w1.length(), N = w2.length();
1917
vector<vector<int>> dp(M, vector<int>(N, 0));
20-
for (size_t i = 0; i <= M; ++i) {
21-
dp[i][0] = i;
22-
}
23-
for (size_t i = 0; i <= N; ++i) {
24-
dp[0][i] = i;
25-
}
18+
for (size_t i = 0; i <= M; ++i) { dp[i][0] = i; }
19+
for (size_t i = 0; i <= N; ++i) { dp[0][i] = i; }
2620

2721
// print_dp(dp);
2822

@@ -41,7 +35,7 @@ class Solution {
4135
}
4236
};
4337

44-
int main(int argc, char const* argv[]) {
38+
int main(int argc, char const *argv[]) {
4539
Solution solution;
4640
cout << solution.minDistance("horse", "ros");
4741
}

src/first-missing-positive.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using namespace std;
44

55
class Solution {
6-
public:
7-
int firstMissingPositive(const vector<int>& numbers) {
6+
public:
7+
int firstMissingPositive(const vector<int> &numbers) {
88
vector<bool> vec(numbers.size(), false);
99
for (const int value : numbers) {
1010
if (value > 0 && value <= numbers.size()) vec[value - 1] = true;
@@ -16,7 +16,7 @@ class Solution {
1616
}
1717
};
1818

19-
int main(int argc, char const* argv[]) {
19+
int main(int argc, char const *argv[]) {
2020
Solution solution;
2121
cout << solution.firstMissingPositive({2, 3, 4, 5, -1});
2222
}

0 commit comments

Comments
 (0)