Skip to content

Commit 4a1ea8f

Browse files
committed
style: format all cpp code
1 parent e350ace commit 4a1ea8f

File tree

1,663 files changed

+6117
-9051
lines changed

Some content is hidden

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

1,663 files changed

+6117
-9051
lines changed

lcci/04.01.Route Between Nodes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public:
149149
bool findWhetherExistsPath(int n, vector<vector<int>>& graph, int start, int target) {
150150
unordered_map<int, vector<int>> g;
151151
for (auto& e : graph) g[e[0]].push_back(e[1]);
152-
unordered_set<int> vis{{start}};
152+
unordered_set<int> vis {{start}};
153153
return dfs(start, target, g, vis);
154154
}
155155

lcci/04.01.Route Between Nodes/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public:
148148
bool findWhetherExistsPath(int n, vector<vector<int>>& graph, int start, int target) {
149149
unordered_map<int, vector<int>> g;
150150
for (auto& e : graph) g[e[0]].push_back(e[1]);
151-
unordered_set<int> vis{{start}};
151+
unordered_set<int> vis {{start}};
152152
return dfs(start, target, g, vis);
153153
}
154154

lcof/面试题68 - II. 二叉树的最近公共祖先/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ public:
170170
}
171171
}
172172
};
173-
174173
```
175174

176175
### **Go**

lcof2/剑指 Offer II 063. 替换单词/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,11 @@ public:
212212
vector<string> words;
213213
string ss;
214214
while (is >> ss) words.push_back(ss);
215-
for (int i = 0; i < words.size(); ++i)
216-
{
215+
for (int i = 0; i < words.size(); ++i) {
217216
string word = words[i];
218-
for (int j = 1; j <= word.size(); ++j)
219-
{
217+
for (int j = 1; j <= word.size(); ++j) {
220218
string t = word.substr(0, j);
221-
if (s.count(t))
222-
{
219+
if (s.count(t)) {
223220
words[i] = t;
224221
break;
225222
}

lcof2/剑指 Offer II 119. 最长连续序列/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,19 @@ class Solution {
161161
```cpp
162162
class Solution {
163163
public:
164-
int longestConsecutive(vector<int> &nums) {
164+
int longestConsecutive(vector<int>& nums) {
165165
int n = nums.size();
166166
if (n < 2)
167167
return n;
168168
sort(nums.begin(), nums.end());
169169
int res = 1, t = 1;
170-
for (int i = 1; i < n; ++i)
171-
{
170+
for (int i = 1; i < n; ++i) {
172171
if (nums[i] == nums[i - 1])
173172
continue;
174-
if (nums[i] - nums[i - 1] == 1)
175-
{
173+
if (nums[i] - nums[i - 1] == 1) {
176174
++t;
177175
res = max(res, t);
178-
}
179-
else
180-
{
176+
} else {
181177
t = 1;
182178
}
183179
}

run_format.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
import os.path
2+
import re
23

34
path = os.getcwd()
4-
for root, dirs, files in os.walk(path):
5-
for name in files:
6-
if name.endswith('.cpp') or name.endswith('.c'):
7-
local_path = root + '/' + name
8-
command = f'clang-format -i --style=file "{local_path}"'
9-
print(command)
10-
os.system(command)
5+
6+
7+
def format_cpp_file():
8+
for root, __import__, files in os.walk(path):
9+
for name in files:
10+
if name.endswith('.cpp') or name.endswith('.c'):
11+
local_path = root + '/' + name
12+
command = f'clang-format -i --style=file "{local_path}"'
13+
print(command)
14+
os.system(command)
15+
16+
17+
def format_readme_cpp_code_block():
18+
for root, _, files in os.walk(path):
19+
for name in files:
20+
if name.endswith('.md'):
21+
p1 = root + '/' + name
22+
with open(p1, 'r', encoding='utf-8') as f:
23+
content = f.read()
24+
x = content
25+
res = re.search('```cpp(.*?)```', content, re.S)
26+
if not res:
27+
continue
28+
print(p1)
29+
res = res.group(1)
30+
p2 = root + "/tmp.cpp"
31+
with open(p2, 'w', encoding='utf-8') as f:
32+
f.write(res)
33+
command = f'clang-format -i --style=file "{p2}"'
34+
os.system(command)
35+
with open(p2, 'r', encoding='utf-8') as f:
36+
content = f.read()
37+
content = x.replace(res, content)
38+
with open(p1, 'w', encoding='utf-8') as f:
39+
f.write(content)
40+
os.remove(p2)
41+
42+
43+
if __name__ == '__main__':
44+
format_readme_cpp_code_block()

solution/0100-0199/0127.Word Ladder/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,16 @@ class Solution {
259259
public:
260260
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
261261
unordered_set<string> words(wordList.begin(), wordList.end());
262-
queue<string> q{{beginWord}};
262+
queue<string> q {{beginWord}};
263263
int ans = 1;
264-
while (!q.empty())
265-
{
264+
while (!q.empty()) {
266265
++ans;
267-
for (int i = q.size(); i > 0; --i)
268-
{
266+
for (int i = q.size(); i > 0; --i) {
269267
string s = q.front();
270268
q.pop();
271-
for (int j = 0; j < s.size(); ++j)
272-
{
269+
for (int j = 0; j < s.size(); ++j) {
273270
char ch = s[j];
274-
for (char k = 'a'; k <= 'z'; ++k)
275-
{
271+
for (char k = 'a'; k <= 'z'; ++k) {
276272
s[j] = k;
277273
if (!words.count(s)) continue;
278274
if (s == endWord) return ans;

solution/0100-0199/0127.Word Ladder/README_EN.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,16 @@ class Solution {
210210
public:
211211
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
212212
unordered_set<string> words(wordList.begin(), wordList.end());
213-
queue<string> q{{beginWord}};
213+
queue<string> q {{beginWord}};
214214
int ans = 1;
215-
while (!q.empty())
216-
{
215+
while (!q.empty()) {
217216
++ans;
218-
for (int i = q.size(); i > 0; --i)
219-
{
217+
for (int i = q.size(); i > 0; --i) {
220218
string s = q.front();
221219
q.pop();
222-
for (int j = 0; j < s.size(); ++j)
223-
{
220+
for (int j = 0; j < s.size(); ++j) {
224221
char ch = s[j];
225-
for (char k = 'a'; k <= 'z'; ++k)
226-
{
222+
for (char k = 'a'; k <= 'z'; ++k) {
227223
s[j] = k;
228224
if (!words.count(s)) continue;
229225
if (s == endWord) return ans;

solution/0100-0199/0128.Longest Consecutive Sequence/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,19 @@ class Solution {
155155
```cpp
156156
class Solution {
157157
public:
158-
int longestConsecutive(vector<int> &nums) {
158+
int longestConsecutive(vector<int>& nums) {
159159
int n = nums.size();
160160
if (n < 2)
161161
return n;
162162
sort(nums.begin(), nums.end());
163163
int res = 1, t = 1;
164-
for (int i = 1; i < n; ++i)
165-
{
164+
for (int i = 1; i < n; ++i) {
166165
if (nums[i] == nums[i - 1])
167166
continue;
168-
if (nums[i] - nums[i - 1] == 1)
169-
{
167+
if (nums[i] - nums[i - 1] == 1) {
170168
++t;
171169
res = max(res, t);
172-
}
173-
else
174-
{
170+
} else {
175171
t = 1;
176172
}
177173
}

solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,19 @@ class Solution {
132132
```cpp
133133
class Solution {
134134
public:
135-
int longestConsecutive(vector<int> &nums) {
135+
int longestConsecutive(vector<int>& nums) {
136136
int n = nums.size();
137137
if (n < 2)
138138
return n;
139139
sort(nums.begin(), nums.end());
140140
int res = 1, t = 1;
141-
for (int i = 1; i < n; ++i)
142-
{
141+
for (int i = 1; i < n; ++i) {
143142
if (nums[i] == nums[i - 1])
144143
continue;
145-
if (nums[i] - nums[i - 1] == 1)
146-
{
144+
if (nums[i] - nums[i - 1] == 1) {
147145
++t;
148146
res = max(res, t);
149-
}
150-
else
151-
{
147+
} else {
152148
t = 1;
153149
}
154150
}

0 commit comments

Comments
 (0)