Skip to content

Commit

Permalink
init workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Jul 27, 2022
1 parent 6a588b2 commit 48d9138
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 3 deletions.
166 changes: 166 additions & 0 deletions .github/workflows/merge-readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import os
import re
import sys


def merge_md_block(main, branch):
new_md = []
main_h2 = dict()
branch_h2 = dict()
main_h2_block, main_h2_block_title = [], None
branch_h2_block, branch_h2_block_title = [], None
# generate main h2 block
for line in main:
if len(line) > 3 and line[:3] == "## ":
if main_h2_block_title is not None:
main_h2[main_h2_block_title] = main_h2_block
main_h2_block = [line]
main_h2_block_title = line
else:
main_h2_block.append(line)
if main_h2_block_title is not None:
main_h2[main_h2_block_title] = main_h2_block
# generate branch h2 block
for line in branch:
if len(line) > 3 and line[:3] == "## ":
if branch_h2_block_title is not None:
branch_h2[branch_h2_block_title] = branch_h2_block
branch_h2_block = [line]
branch_h2_block_title = line
else:
branch_h2_block.append(line)
if branch_h2_block_title is not None:
branch_h2[branch_h2_block_title] = branch_h2_block
# merge h2 block
for key, value in main_h2.items():
if branch_h2.get(key) is not None:
tmp = merge_h2_block(value, branch_h2[key])
for line in tmp:
new_md.append(line)
else:
for line in value:
new_md.append(line)
for key, value in branch_h2.items():
if main_h2.get(key) is None:
for line in value:
new_md.append(line)
return new_md


def merge_h2_block(main, branch):
new_md = [main[0]]
main_h3 = dict()
branch_h3 = dict()
main_h3_block, main_h3_block_title = [], None
branch_h3_block, branch_h3_block_title = [], None
# generate main h3 block
for line in main[1:]:
if len(line) > 4 and line[:4] == "### ":
if main_h3_block_title is not None:
main_h3[main_h3_block_title] = main_h3_block
main_h3_block = [line]
main_h3_block_title = line
else:
main_h3_block.append(line)
if main_h3_block_title is not None:
main_h3[main_h3_block_title] = main_h3_block
# generate branch h3 block
for line in branch[1:]:
if len(line) > 4 and line[:4] == "### ":
if branch_h3_block_title is not None:
branch_h3[branch_h3_block_title] = branch_h3_block
branch_h3_block = [line]
branch_h3_block_title = line
else:
branch_h3_block.append(line)
if branch_h3_block_title is not None:
branch_h3[branch_h3_block_title] = branch_h3_block
# merge h3 block
for key, value in main_h3.items():
if branch_h3.get(key) is not None:
tmp = merge_h3_block(value, branch_h3[key])
for line in tmp:
new_md.append(line)
else:
for line in value:
new_md.append(line)
for key, value in branch_h3.items():
if main_h3.get(key) is None:
for line in value:
new_md.append(line)
return new_md


def merge_h3_block(main, branch):
new_ul = []
new_text = [main[0]]
for line in main[1:]:
if len(line) > 2 and line[:2] == "* ":
new_ul.append(line)
else:
new_text.append(line)
for line in branch[1:]:
if len(line) > 2 and line[:2] == "* ":
new_ul.append(line)
else:
new_text.append(line)
return new_text


def change_url(base_url, line):
r = re.compile("^\* \[.*\]\((.*)\).*$")
res = r.match(line)
if res is not None:
relative_url = res.group(1)
line = line.replace(relative_url, os.path.join(base_url, relative_url))
return line


def load_md(path, is_change_url=False):
md = None
dir = os.path.dirname(path)
with open("../../" + path, encoding='utf-8') as file:
md = file.readlines()
for i in range(len(md)-1, -1, -1):
if md[i] == "\n":
del(md[i])
else:
if is_change_url:
md[i] = change_url(dir, md[i])
return md


def merge_mds(paths):
main_md = ''
for path in paths:
branch_md = load_md(path, True)
main_md = merge_md_block(main_md, branch_md)
return main_md


def main():
# the path of base.md
main_dir = sys.argv[1]
# the dir of branchs' parent dir
branch_dir = sys.argv[2]
# ths path of README.md to save
save_dir = sys.argv[3]
# merge branch mds
branchs = os.listdir(branch_dir)
branch_path = []
for i in branchs:
path = "/".join([branch_dir, i, "README.md"])
if os.path.exists(path):
branch_path.append(path)
branch_md = merge_mds(branch_path)
# load main md
main_md = load_md(main_dir)
# merge main-md & branch-mds
main_md = [*main_md, *branch_md]
# save md
with open(save_dir, encoding='utf-8', mode='w') as file:
file.write("".join(main_md))


if __name__ == "__main__":
main()
33 changes: 33 additions & 0 deletions .github/workflows/merge-readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: merge-readme
on:
push:
branches:
- '**'
- '!main'
jobs:
merge-readmes:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v4.1.0
with:
python-version: 3.7
- name: clone
run: |
mkdir tree
git clone https://github.com/baowj-678/leetcode.git -b go tree/go
git clone https://github.com/baowj-678/leetcode.git -b java tree/java
git clone https://github.com/baowj-678/leetcode.git -b cpp tree/cpp
git clone https://github.com/baowj-678/leetcode.git -b python tree/python
git clone https://github.com/baowj-678/leetcode.git -b sql tree/sql
git clone https://${{secrets.PAT}}@github.com/baowj-678/leetcode.git main
- name: merge
run: |
python main/.github/workflows/merge-readme.py main/base.md tree main/README.md
- name: git-push
run: |
cd main/
git config --global user.email "bwj_678@qq.com"
git config --global user.name "leetcode-merge-robot"
git add README.md
git commit -m 'auto merge README.md'
git push https://${{secrets.PAT}}@github.com/baowj-678/leetcode.git
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# LeetCode代码
# LeetCode Java代码

【易】:简单,能做出;【中】:需要思考做出;【难】:做不出;

## 经典题目
## 题型分类
### 差分数组
* [731. 我的日程安排表 II](./src/main/java/com/leetcode/731. 我的日程安排表 II.java)
* [731. 我的日程安排表 II](src/main/java/com/leetcode/731.%20我的日程安排表%20II.java)

### 贪心算法
* [757. 设置交集大小至少为2](./src/main/java/com/leetcode/757. 设置交集大小至少为2.java)【难,重要】
* [757. 设置交集大小至少为2](src/main/java/com/leetcode/757.%20设置交集大小至少为2.java)【难,重要】

### 拓扑排序
* [剑指 Offer II 115. 重建序列.java](src/main/java/com/leetcode/剑指%20Offer%20II%20115.%20重建序列.java)【难,重要】

## 知识点总结
* HashMap遍历无顺序,TreeMap遍历按照Key字典序升序;
29 changes: 29 additions & 0 deletions src/main/java/com/leetcode/剑指 Offer II 115. 重建序列.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.leetcode;

import java.util.Arrays;

class SolutionOffer115 {
public boolean sequenceReconstruction(int[] nums, int[][] sequences) {
boolean[] flags = new boolean[nums.length+1];
int[] indexs = new int[nums.length+1];
for (int i = 0; i < nums.length; i++) {
indexs[nums[i]] = i;
}
Arrays.fill(flags, false);
for (int[] sequence : sequences) {
for (int i = 0; i+1 < sequence.length; i++) {
int index = indexs[sequence[i]];
if (nums[index+1] == sequence[i+1]) {
flags[sequence[i]] = true;
}
}
}
int cnt = 0;
for (int i = 1; i < nums.length; i++) {
if (!flags[i]) {
cnt++;
}
}
return cnt == 1;
}
}

0 comments on commit 48d9138

Please sign in to comment.