Skip to content

Commit

Permalink
chore(C++): Update to use Google's clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
euchangxian committed Oct 8, 2024
1 parent db9644b commit dc4327a
Show file tree
Hide file tree
Showing 306 changed files with 1,390 additions and 1,296 deletions.
4 changes: 2 additions & 2 deletions C++/0001-TwoSum/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> indexOf;
for (int i = 0; i < nums.size(); ++i) {
int goal = target - nums[i];
Expand Down
18 changes: 9 additions & 9 deletions C++/0002-AddTwoNumbers/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
#include <vector>
struct ListNode {
int val;
ListNode *next;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

using namespace std;
class Solution {
private:
ListNode *addWithCarry(ListNode *l1, ListNode *l2, int carry) {
private:
ListNode* addWithCarry(ListNode* l1, ListNode* l2, int carry) {
if (!l1 && !l2 && carry == 0) {
return nullptr; // end of number
return nullptr; // end of number
}

int l1Val = 0;
int l2Val = 0;
ListNode *l1Next = nullptr;
ListNode *l2Next = nullptr;
ListNode* l1Next = nullptr;
ListNode* l2Next = nullptr;

if (l1) {
l1Val = l1->val;
Expand All @@ -43,8 +43,8 @@ class Solution {
return new ListNode(sum % 10, addWithCarry(l1Next, l2Next, sum / 10));
}

public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
return addWithCarry(l1, l2, 0);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace std;
class Solution {
public:
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> unique;
int result = 0;
Expand Down
4 changes: 2 additions & 2 deletions C++/0004-FindMedianOfTwoSortedArrays/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

using namespace std;
class Solution {
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
// Intuition: Finding the median is equivalent to finding the right "cut"
// point in both arrays that divides all elements into two equal halves.
//
Expand Down
10 changes: 5 additions & 5 deletions C++/0005-LongestPalindromicSubstring/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

using namespace std;
class Solution {
private:
pair<int, int> expandCentre(const string &s, int left, int right) {
private:
pair<int, int> expandCentre(const string& s, int left, int right) {
while (left >= 0 && right < s.length() && s[left] == s[right]) {
--left;
++right;
Expand All @@ -20,15 +20,15 @@ class Solution {
return {left + 1, right - 1};
}

public:
public:
string longestPalindrome(string s) {
int n = s.length();

int start = 0;
int maxLength = 1;
for (int i = 0; i < n; ++i) {
const auto &[oddL, oddR] = expandCentre(s, i, i);
const auto &[evenL, evenR] = expandCentre(s, i, i + 1);
const auto& [oddL, oddR] = expandCentre(s, i, i);
const auto& [evenL, evenR] = expandCentre(s, i, i + 1);

if (oddR - oddL + 1 > maxLength) {
start = oddL;
Expand Down
4 changes: 2 additions & 2 deletions C++/0006-ZigZagConversion/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace std;
class Solution {
public:
public:
string convert(string s, int numRows) {
if (numRows < 2) {
return s;
Expand All @@ -36,7 +36,7 @@ class Solution {
}

string result;
for (const auto &str : rowStrings) {
for (const auto& str : rowStrings) {
result += str;
}

Expand Down
6 changes: 3 additions & 3 deletions C++/0007-ReverseInteger/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

using namespace std;
class Solution {
private:
private:
const int INT_MAX_THRESHOLD = INT_MAX / 10;
const int INT_MIN_THRESHOLD = INT_MIN / 10;

public:
public:
int reverse(int x) {
int result = 0;

Expand All @@ -25,7 +25,7 @@ class Solution {
return 0;
}

result = (result * 10) + (x % 10); // shift left, add last digit
result = (result * 10) + (x % 10); // shift left, add last digit
x /= 10;
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion C++/0008-StringToIntegerAtoi/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace std;
class Solution {
public:
public:
int myAtoi(string s) {
int i = 0;
int n = s.length();
Expand Down
12 changes: 6 additions & 6 deletions C++/0010-RegularExpressionMatching/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace std;
class Solution {
bool match(char c, char p) { return c == p || p == '.'; }

public:
public:
bool isMatch(string s, string p) {
// '.' matches any single character
// '*' matches zero or more of the preceding element.
Expand Down Expand Up @@ -46,17 +46,17 @@ class Solution {
// dp[i][j] = dp[i - 1][j] (take multiple times)
// && (s[i-1] == p[j-1] || p[j-1] == '.') (char must match)
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
dp[0][0] = true; // vacuously true
dp[0][0] = true; // vacuously true
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '*') {
dp[0][j] = dp[0][j - 2]; // vacuously true for kleene star
dp[0][j] = dp[0][j - 2]; // vacuously true for kleene star
}
}

for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (match(s[i - 1], p[j - 1])) {
dp[i][j] = dp[i - 1][j - 1]; // && true
dp[i][j] = dp[i - 1][j - 1]; // && true
continue;
}

Expand All @@ -68,8 +68,8 @@ class Solution {
}

// if '*'
char preceding = p[j - 2]; // no need out-of-bounds check
dp[i][j] = dp[i][j - 2]; // repeat preceding character 0 times
char preceding = p[j - 2]; // no need out-of-bounds check
dp[i][j] = dp[i][j - 2]; // repeat preceding character 0 times

if (match(s[i - 1], preceding)) {
// while s[i-1] matches the preceding pattern character,
Expand Down
4 changes: 2 additions & 2 deletions C++/0011-ContainerWithMostWater/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using namespace std;
class Solution {
public:
int maxArea(vector<int> &height) {
public:
int maxArea(vector<int>& height) {
int left = 0;
int right = height.size() - 1;

Expand Down
9 changes: 4 additions & 5 deletions C++/0014-LongestCommonPrefix/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using namespace std;

class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) {
return "";
}
Expand All @@ -17,12 +17,11 @@ class Solution {
prefix[j] == strs[i][j]) {
++j;
}
prefix = prefix.substr(0, j); // Adjust the prefix
prefix = prefix.substr(0, j); // Adjust the prefix
if (prefix.empty()) {
break; // Early exit if there's no common prefix
break; // Early exit if there's no common prefix
}
}
return prefix;
}
};

4 changes: 2 additions & 2 deletions C++/0015-ThreeSum/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());

vector<vector<int>> result;
Expand Down
11 changes: 7 additions & 4 deletions C++/0017-LetterCombinationsOfAPhoneNumber/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

using namespace std;
class Solution {
private:
private:
// Note that unlike usual backtracking solutions where the reference of
// the current vector/string is passed in like &current, this one requires
// pass-by-value.
void dfs(string const &digits, vector<vector<char>> const &mappings,
vector<string> &combinations, string current, int i) {
void dfs(string const& digits,
vector<vector<char>> const& mappings,
vector<string>& combinations,
string current,
int i) {
if (i >= digits.length()) {
combinations.push_back(current);
return;
Expand All @@ -29,7 +32,7 @@ class Solution {
}
}

public:
public:
vector<string> letterCombinations(string digits) {
if (digits.empty()) {
return {};
Expand Down
12 changes: 6 additions & 6 deletions C++/0019-RemoveNthNodeFromEndOfList/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
#include <vector>
struct ListNode {
int val;
ListNode *next;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

using namespace std;
class Solution {
private:
ListNode *removeFromBack(ListNode *curr, int const n, int &k) {
private:
ListNode* removeFromBack(ListNode* curr, int const n, int& k) {
if (!curr) {
return nullptr;
}
Expand All @@ -34,8 +34,8 @@ class Solution {
return curr;
}

public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int k = 0;
return removeFromBack(head, n, k);
}
Expand Down
4 changes: 2 additions & 2 deletions C++/0020-ValidParentheses/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

using namespace std;
class Solution {
public:
public:
bool isValid(string s) {
stack<char> expected;

for (char const &c : s) {
for (char const& c : s) {
if (c == '(') {
expected.push(')');
continue;
Expand Down
10 changes: 5 additions & 5 deletions C++/0021-MergeTwoSortedLists/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
#include <vector>
struct ListNode {
int val;
ListNode *next;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

using namespace std;
class Solution {
private:
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
private:
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (list1 == nullptr) {
return list2;
}
Expand Down
9 changes: 4 additions & 5 deletions C++/0022-GenerateParentheses/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace std;
class Solution {
private:
private:
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) {
Expand All @@ -20,7 +20,7 @@ class Solution {
return result;
}

public:
public:
// Follows the sequence of Catalan Numbers, which has two rules:
// 1. "" is in the Set
// 2. if a and b are in the Set, then (a)b is in the set.
Expand All @@ -39,9 +39,8 @@ class Solution {
for (int i = 1; i <= n; ++i) {
// For every combination of j, (i - 1 - j)
for (int j = 0; j < i; ++j) {

for (string const &a : dp[j]) {
for (string const &b : dp[i - j - 1]) {
for (string const& a : dp[j]) {
for (string const& b : dp[i - j - 1]) {
dp[i].push_back('(' + a + ')' + b);
}
}
Expand Down
Loading

0 comments on commit dc4327a

Please sign in to comment.