Skip to content

Commit

Permalink
style: format all cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Aug 13, 2022
1 parent e350ace commit 4a1ea8f
Show file tree
Hide file tree
Showing 1,663 changed files with 6,117 additions and 9,051 deletions.
2 changes: 1 addition & 1 deletion lcci/04.01.Route Between Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public:
bool findWhetherExistsPath(int n, vector<vector<int>>& graph, int start, int target) {
unordered_map<int, vector<int>> g;
for (auto& e : graph) g[e[0]].push_back(e[1]);
unordered_set<int> vis{{start}};
unordered_set<int> vis {{start}};
return dfs(start, target, g, vis);
}

Expand Down
2 changes: 1 addition & 1 deletion lcci/04.01.Route Between Nodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public:
bool findWhetherExistsPath(int n, vector<vector<int>>& graph, int start, int target) {
unordered_map<int, vector<int>> g;
for (auto& e : graph) g[e[0]].push_back(e[1]);
unordered_set<int> vis{{start}};
unordered_set<int> vis {{start}};
return dfs(start, target, g, vis);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ public:
}
}
};

```

### **Go**
Expand Down
9 changes: 3 additions & 6 deletions lcof2/剑指 Offer II 063. 替换单词/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,11 @@ public:
vector<string> words;
string ss;
while (is >> ss) words.push_back(ss);
for (int i = 0; i < words.size(); ++i)
{
for (int i = 0; i < words.size(); ++i) {
string word = words[i];
for (int j = 1; j <= word.size(); ++j)
{
for (int j = 1; j <= word.size(); ++j) {
string t = word.substr(0, j);
if (s.count(t))
{
if (s.count(t)) {
words[i] = t;
break;
}
Expand Down
12 changes: 4 additions & 8 deletions lcof2/剑指 Offer II 119. 最长连续序列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,19 @@ class Solution {
```cpp
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
int longestConsecutive(vector<int>& nums) {
int n = nums.size();
if (n < 2)
return n;
sort(nums.begin(), nums.end());
int res = 1, t = 1;
for (int i = 1; i < n; ++i)
{
for (int i = 1; i < n; ++i) {
if (nums[i] == nums[i - 1])
continue;
if (nums[i] - nums[i - 1] == 1)
{
if (nums[i] - nums[i - 1] == 1) {
++t;
res = max(res, t);
}
else
{
} else {
t = 1;
}
}
Expand Down
48 changes: 41 additions & 7 deletions run_format.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import os.path
import re

path = os.getcwd()
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith('.cpp') or name.endswith('.c'):
local_path = root + '/' + name
command = f'clang-format -i --style=file "{local_path}"'
print(command)
os.system(command)


def format_cpp_file():
for root, __import__, files in os.walk(path):
for name in files:
if name.endswith('.cpp') or name.endswith('.c'):
local_path = root + '/' + name
command = f'clang-format -i --style=file "{local_path}"'
print(command)
os.system(command)


def format_readme_cpp_code_block():
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.md'):
p1 = root + '/' + name
with open(p1, 'r', encoding='utf-8') as f:
content = f.read()
x = content
res = re.search('```cpp(.*?)```', content, re.S)
if not res:
continue
print(p1)
res = res.group(1)
p2 = root + "/tmp.cpp"
with open(p2, 'w', encoding='utf-8') as f:
f.write(res)
command = f'clang-format -i --style=file "{p2}"'
os.system(command)
with open(p2, 'r', encoding='utf-8') as f:
content = f.read()
content = x.replace(res, content)
with open(p1, 'w', encoding='utf-8') as f:
f.write(content)
os.remove(p2)


if __name__ == '__main__':
format_readme_cpp_code_block()
14 changes: 5 additions & 9 deletions solution/0100-0199/0127.Word Ladder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,16 @@ class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> words(wordList.begin(), wordList.end());
queue<string> q{{beginWord}};
queue<string> q {{beginWord}};
int ans = 1;
while (!q.empty())
{
while (!q.empty()) {
++ans;
for (int i = q.size(); i > 0; --i)
{
for (int i = q.size(); i > 0; --i) {
string s = q.front();
q.pop();
for (int j = 0; j < s.size(); ++j)
{
for (int j = 0; j < s.size(); ++j) {
char ch = s[j];
for (char k = 'a'; k <= 'z'; ++k)
{
for (char k = 'a'; k <= 'z'; ++k) {
s[j] = k;
if (!words.count(s)) continue;
if (s == endWord) return ans;
Expand Down
14 changes: 5 additions & 9 deletions solution/0100-0199/0127.Word Ladder/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,16 @@ class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> words(wordList.begin(), wordList.end());
queue<string> q{{beginWord}};
queue<string> q {{beginWord}};
int ans = 1;
while (!q.empty())
{
while (!q.empty()) {
++ans;
for (int i = q.size(); i > 0; --i)
{
for (int i = q.size(); i > 0; --i) {
string s = q.front();
q.pop();
for (int j = 0; j < s.size(); ++j)
{
for (int j = 0; j < s.size(); ++j) {
char ch = s[j];
for (char k = 'a'; k <= 'z'; ++k)
{
for (char k = 'a'; k <= 'z'; ++k) {
s[j] = k;
if (!words.count(s)) continue;
if (s == endWord) return ans;
Expand Down
12 changes: 4 additions & 8 deletions solution/0100-0199/0128.Longest Consecutive Sequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,19 @@ class Solution {
```cpp
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
int longestConsecutive(vector<int>& nums) {
int n = nums.size();
if (n < 2)
return n;
sort(nums.begin(), nums.end());
int res = 1, t = 1;
for (int i = 1; i < n; ++i)
{
for (int i = 1; i < n; ++i) {
if (nums[i] == nums[i - 1])
continue;
if (nums[i] - nums[i - 1] == 1)
{
if (nums[i] - nums[i - 1] == 1) {
++t;
res = max(res, t);
}
else
{
} else {
t = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,19 @@ class Solution {
```cpp
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
int longestConsecutive(vector<int>& nums) {
int n = nums.size();
if (n < 2)
return n;
sort(nums.begin(), nums.end());
int res = 1, t = 1;
for (int i = 1; i < n; ++i)
{
for (int i = 1; i < n; ++i) {
if (nums[i] == nums[i - 1])
continue;
if (nums[i] - nums[i - 1] == 1)
{
if (nums[i] - nums[i - 1] == 1) {
++t;
res = max(res, t);
}
else
{
} else {
t = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ function twoSum(numbers: number[], target: number): number[] {
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for (int i = 0, n = numbers.size(); i < n - 1; ++i)
{
for (int i = 0, n = numbers.size(); i < n - 1; ++i) {
int x = target - numbers[i];
int j = lower_bound(numbers.begin() + i + 1, numbers.end(), x) - numbers.begin();
if (j != n && numbers[j] == x) return {i + 1, j + 1};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ Binary search:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for (int i = 0, n = numbers.size(); i < n - 1; ++i)
{
for (int i = 0, n = numbers.size(); i < n - 1; ++i) {
int x = target - numbers[i];
int j = lower_bound(numbers.begin() + i + 1, numbers.end(), x) - numbers.begin();
if (j != n && numbers[j] == x) return {i + 1, j + 1};
Expand Down
12 changes: 6 additions & 6 deletions solution/0200-0299/0207.Course Schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> g(numCourses);
vector<int> indeg(numCourses);
for (auto& p : prerequisites)
{
for (auto& p : prerequisites) {
int a = p[0], b = p[1];
g[b].push_back(a);
++indeg[a];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i);
for (int i = 0; i < numCourses; ++i)
if (indeg[i] == 0) q.push(i);
int cnt = 0;
while (!q.empty())
{
while (!q.empty()) {
int i = q.front();
q.pop();
++cnt;
for (int j : g[i]) if (--indeg[j] == 0) q.push(j);
for (int j : g[i])
if (--indeg[j] == 0) q.push(j);
}
return cnt == numCourses;
}
Expand Down
12 changes: 6 additions & 6 deletions solution/0200-0299/0207.Course Schedule/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> g(numCourses);
vector<int> indeg(numCourses);
for (auto& p : prerequisites)
{
for (auto& p : prerequisites) {
int a = p[0], b = p[1];
g[b].push_back(a);
++indeg[a];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i);
for (int i = 0; i < numCourses; ++i)
if (indeg[i] == 0) q.push(i);
int cnt = 0;
while (!q.empty())
{
while (!q.empty()) {
int i = q.front();
q.pop();
++cnt;
for (int j : g[i]) if (--indeg[j] == 0) q.push(j);
for (int j : g[i])
if (--indeg[j] == 0) q.push(j);
}
return cnt == numCourses;
}
Expand Down
10 changes: 5 additions & 5 deletions solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ private:

Trie* searchPrefix(string s) {
Trie* node = this;
for (char c : s)
{
for (char c : s) {
int idx = c - 'a';
if (!node->children[idx]) return nullptr;
node = node->children[idx];
Expand All @@ -265,12 +264,13 @@ private:
}

public:
Trie() : children(26), isEnd(false) {}
Trie()
: children(26)
, isEnd(false) { }

void insert(string word) {
Trie* node = this;
for (char c : word)
{
for (char c : word) {
int idx = c - 'a';
if (!node->children[idx]) node->children[idx] = new Trie();
node = node->children[idx];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ private:

Trie* searchPrefix(string s) {
Trie* node = this;
for (char c : s)
{
for (char c : s) {
int idx = c - 'a';
if (!node->children[idx]) return nullptr;
node = node->children[idx];
Expand All @@ -228,12 +227,13 @@ private:
}

public:
Trie() : children(26), isEnd(false) {}
Trie()
: children(26)
, isEnd(false) { }

void insert(string word) {
Trie* node = this;
for (char c : word)
{
for (char c : word) {
int idx = c - 'a';
if (!node->children[idx]) node->children[idx] = new Trie();
node = node->children[idx];
Expand Down
6 changes: 2 additions & 4 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,10 @@ public:
vector<int> s(n + 1);
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
int ans = n + 1;
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
int t = s[i] + target;
auto p = lower_bound(s.begin(), s.end(), t);
if (p != s.end())
{
if (p != s.end()) {
int j = p - s.begin();
ans = min(ans, j - i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ public:
vector<int> s(n + 1);
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
int ans = n + 1;
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
int t = s[i] + target;
auto p = lower_bound(s.begin(), s.end(), t);
if (p != s.end())
{
if (p != s.end()) {
int j = p - s.begin();
ans = min(ans, j - i);
}
Expand Down
Loading

0 comments on commit 4a1ea8f

Please sign in to comment.