Skip to content

Commit de92acb

Browse files
committed
output to specific dir
1 parent f5a1bac commit de92acb

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

scripts/main.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import argparse
66
from datetime import datetime
77

8+
import frontmatter
9+
from slugify import slugify
10+
811
from util import par_dir, mkdir_p
912
from leetcode import Leetcode
1013
from ojhtml2markdown import problem2md
@@ -20,6 +23,8 @@ def curr_time():
2023
parser = argparse.ArgumentParser(description='Helper for GitBook algorithm')
2124
parser.add_argument('--new', type=str, dest='new',
2225
help='create new post with given leetcode/lintcode url.')
26+
parser.add_argument('--dir', type=str, dest='dir',
27+
help='create md under dir.')
2328
parser.add_argument('--update', nargs='*', dest='update',
2429
help='update post with given title in post and summary.')
2530
parser.add_argument('--migrate', type=str, dest='migrate',
@@ -31,8 +36,24 @@ def curr_time():
3136

3237
ROOTDIR = par_dir(BASEDIR)
3338
raw_url = args.new
39+
problem_md = ''
40+
problem_slug = ''
3441
if raw_url.startswith('https://leetcode'):
3542
leetcode = Leetcode()
3643
problem = leetcode.get_problem_all(raw_url)
44+
problem_slug = slugify(problem['title'], separator="_")
3745
problem_md = problem2md(problem)
38-
print(problem_md)
46+
47+
if args.dir:
48+
post_dir = os.path.join(ROOTDIR, args.dir)
49+
post_fn = os.path.join(post_dir, problem_slug + '.md')
50+
summary_path = args.dir.strip('/').split('/')[-1] + '/' + problem_slug + '.md'
51+
summary_line = '* [{title}]({path})'.format(title=problem['title'], path=summary_path)
52+
print(summary_line)
53+
mkdir_p(post_dir)
54+
with open(post_fn, 'w', encoding='utf-8') as f:
55+
print('create post file {}...'.format(post_fn))
56+
f.write(problem_md)
57+
58+
if args.fix_summary:
59+
update_summary(ROOTDIR)

zh-hans/math_and_bit_manipulation/palindrome_number.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ There is a more generic way of solving this problem.
3737

3838
## 题解1 - 循环处理首尾数字
3939

40-
题意为判断数字是否为回文,要求为不允许使用额外空间,也就是说不能使用类似数字转字符串的方法直接判断。既然不能使用和数字等长的数组空间,那不借助数组来循环判断首尾数字是否相等总是可以的。接下来的问题就转化为怎么获取数字的首尾数字,获取整数的末尾是非常容易的,对10取模即可,那如何获取整数的首部数字呢?用当前整数除以10的幂(幂的大小和整数的宽度一样)即可。确定好初始和循环终止条件即可。
40+
题意为判断数字是否为回文,要求为不允许使用额外空间,也就是说不能使用类似数字转字符串的方法直接判断。既然不能使用和数字等长的数组空间,那不借助数组来循环判断首尾数字是否相等总是可以的。接下来的问题就转化为怎么获取数字的首尾数字,获取整数的末尾是非常容易的,对10取模即可,那如何获取整数的首部数字呢?用当前整数除以10的幂(幂的大小和整数的宽度一样)即可。确定好初始和循环终止条件即可。
4141

4242
### Java
4343

@@ -64,7 +64,7 @@ class Solution {
6464

6565
### 源码分析
6666

67-
对于32位整数来说,初始化最大的除数为 1000000000, 循环找出适合当前的最大的除数,随后算出首尾的数字并对其进行比对,循环退出条件为首尾不匹配或者除数为1(比对至最后一位).
67+
对于32位整数来说,初始化最大的除数为 1000000000, 循环找出适合当前的最大的除数,随后算出首尾的数字并对其进行比对,循环退出条件为首尾不匹配或者除数为1(比对至最后一位).
6868

6969
### 复杂度分析
7070

@@ -102,4 +102,4 @@ class Solution {
102102

103103
### 复杂度分析
104104

105-
空间复杂度为 $$O(1)$$, 时间复杂度为 $$logN$$.
105+
空间复杂度为 $$O(1)$$, 时间复杂度为 $$logN$$.

0 commit comments

Comments
 (0)