Skip to content

Feat: sword means offer: 64-sum-1-n-nums #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# http://editorconfig.org
# https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties

# 表示是最顶层的配置文件, 发现设为true时, 才会停止查找.editorconfig文件
root = true

[*]
# tab space
indent_style = tab

indent_size = 2
# 设置换行符, 值为lf, cr, crlf
end_of_line = lf
charset = utf-8

# 用一个整数来设置tab缩进的列数。默认是indent_size
tab_width = indent_size

#是否删除行尾的空格
trim_trailing_whitespace = true

#是否在文件的最后插入一个空行
insert_final_newline = true

[*.md]
indent_size = 2
trim_trailing_whitespace = false
insert_final_newline = true

[Makefile]
indent_style = tab
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# 1.0.0 (2020-07-26)
# 1.0.0 (2020-07-29)


### Bug Fixes

* algorithms docs build bug ([c4ed72d](https://github.com/Rain120/awesome-javascript-code-implementation/commit/c4ed72d2da7b0f940a4f7530bdfaaa47631741ff))
* changelog not add ([d69f43f](https://github.com/Rain120/awesome-javascript-code-implementation/commit/d69f43f5112ea7d55242b46ca670ad54741bd940))
* ci bug ([f6185d6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/f6185d6f29e1587bba76f0772433441602bc8db7))
* deploy bug ([568b663](https://github.com/Rain120/awesome-javascript-code-implementation/commit/568b663a5fd6d1b4ab40043e0a693564d7dc3a84))
* gitalk cannot login; feat: manifest ([ee9e237](https://github.com/Rain120/awesome-javascript-code-implementation/commit/ee9e237314451244a13c6c4c13f8a119572b6ecb))
Expand All @@ -14,7 +13,9 @@
* math docs menu ([3958b99](https://github.com/Rain120/awesome-javascript-code-implementation/commit/3958b99b27170aa419b4b8983d7cf62cbae6b724))
* netlify deploy bug ([f1830e9](https://github.com/Rain120/awesome-javascript-code-implementation/commit/f1830e9a07d27b4d6b9c3c67e13272e9064bb322))
* plop create code and docs model(plop name bug) ([8ef0e62](https://github.com/Rain120/awesome-javascript-code-implementation/commit/8ef0e627daadc21842de8a35495c345f9ca70550))
* plop docs code path; changelog; feat: editorconfig ([27073f0](https://github.com/Rain120/awesome-javascript-code-implementation/commit/27073f0aee4027321e798b0d48d900a686df5326))
* readme selected; package.json husky add test ([b228b61](https://github.com/Rain120/awesome-javascript-code-implementation/commit/b228b6194832f909c0b3c7a0c38843bab3b7c63d))
* remove changelog at pre commit ([c49bf26](https://github.com/Rain120/awesome-javascript-code-implementation/commit/c49bf2667ff411786de3bb2442a6a4b8af9a0083))


### Features
Expand Down Expand Up @@ -52,20 +53,22 @@
* plop create code and docs model ([2dece76](https://github.com/Rain120/awesome-javascript-code-implementation/commit/2dece76fbfd83514551dbabadf7c54d73674730d))
* plop new code and docs template ([61a936b](https://github.com/Rain120/awesome-javascript-code-implementation/commit/61a936b58bc52a1cf984dc6156746b43f65da7f1))
* plop new docs or template set alias; update docs about alias ([3722bcc](https://github.com/Rain120/awesome-javascript-code-implementation/commit/3722bccea8605dfef55dd270e7f2ca14a1c03ae7))
* plop new docs or template set alias; update docs about alias ([#21](https://github.com/Rain120/awesome-javascript-code-implementation/issues/21)) ([4334218](https://github.com/Rain120/awesome-javascript-code-implementation/commit/4334218a4ca6635a107fe3baf2d0a17fe3e7458b))
* promise; debugger launch ([cc8ad80](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cc8ad803956e65431cedd97f1ebb333ffe78c28e))
* proxy design ([e615f39](https://github.com/Rain120/awesome-javascript-code-implementation/commit/e615f39497043f4c2357c7366cb13226b57208e6))
* sort -> bubbleSort(algorithm, readme, test) ([36fab7b](https://github.com/Rain120/awesome-javascript-code-implementation/commit/36fab7b029d425ac129b6c4f202b18a9a3852fef))
* sort -> bucket, radix; algorithm -> binaryTree; add some types; docs: update readme; fix: tests format, ([b1d166c](https://github.com/Rain120/awesome-javascript-code-implementation/commit/b1d166c7e56e37f35ab0bc7628186eeae15a4b60))
* sword means offer: 03 find repeat number; fix: plop template; readme title ([cf084d8](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cf084d80099aa5445bd6522a6d8be373ad2c73fb))
* sword means offer: 64-sum-1-n-nums ([9e09cf8](https://github.com/Rain120/awesome-javascript-code-implementation/commit/9e09cf87198dfe22129ae7118e7d24966b235972))
* update docs -> algorithm: binary tree, sort ([5d57bc4](https://github.com/Rain120/awesome-javascript-code-implementation/commit/5d57bc4489551bffeca34102f2fce61b59996873))
* update docs -> design pattern function array object math primitive types utils ([1690af5](https://github.com/Rain120/awesome-javascript-code-implementation/commit/1690af5ed96c46341465f72866732837e5c72200))
* update netlify status ([013b5ca](https://github.com/Rain120/awesome-javascript-code-implementation/commit/013b5ca5847873adabbff93a89daa41500ccf153))
* update readme for plans ([b427b96](https://github.com/Rain120/awesome-javascript-code-implementation/commit/b427b9667cc97c77df75ef0af56eb84b6b4c3ba6))
* update test.yml -> ci.yml and set command ([43b96b6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/43b96b63fc2788a5ea37fa7567c6a6dced4b875f))
* update vdom-diff docs ([06590c5](https://github.com/Rain120/awesome-javascript-code-implementation/commit/06590c56d0df7cbba68bb705cb7e159f6f765c3a))
* update xmind ([cb23933](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cb23933f46152c59a7b08f84d7355b35bba25002))
* update xmind ([71317c6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/71317c67376bc8196f80e25391adfdc9b2496380))
* update xmind ([408ca68](https://github.com/Rain120/awesome-javascript-code-implementation/commit/408ca685142dc425e90be64a284016d57d580893))
* update xmind ([71317c6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/71317c67376bc8196f80e25391adfdc9b2496380))
* update xmind ([4256758](https://github.com/Rain120/awesome-javascript-code-implementation/commit/4256758c5fe0a036ce3ececc43f848e48964d7a0))
* update: plans; fix: readme bug ([d44e559](https://github.com/Rain120/awesome-javascript-code-implementation/commit/d44e559def7834fcccbb19797a1d29bf5cc4e5b7))
* vdom diff algorithm ([3b7779d](https://github.com/Rain120/awesome-javascript-code-implementation/commit/3b7779dcad7e184027d1bddfa6387224c2f009d3))
Expand Down
5 changes: 3 additions & 2 deletions docs/.vuepress/utils/alias.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
"shellSort": "希尔排序",
"sword-means-offer-------": "",
"sword-means-offer": "剑指 Offer",
"03-find-repeat-number": "03. 数组中重复的数字"
}
"03-find-repeat-number": "03. 数组中重复的数字",
"64-sum-1-n-nums": "64 求1 ~ n的和"
}
33 changes: 33 additions & 0 deletions docs/zh/sword-means-offer/64-sum-1-n-nums/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### 题目描述

求 `1 + 2 + ... + n` , 要求 **不能使用** `乘除法`, `for`, `while`, `if`, `else`, `switch`, `case` 等关键字及条件判断语句 `(A ? B : C)` 。


示例 1:

```
输入: n = 3
输出: 6
```

示例 2:
```
输入: n = 9
输出: 45
```

限制:

```
1 <= n <= 10000
```

来源: [力扣 (LeetCode)](https://leetcode-cn.com/problems/qiu-12n-lcof)

## 解题思路 or 实现原理

我们小学就学过 `1 + 2 + ... + n` 的结果就是 $ sum = \frac{(1 + n) * n}{2} $, 但是由于题目限定, 我们不能使用常规方法, 所以我们可以通过 **短路递归** 和 **幂运算移位** 来计算出结果。

## 实现代码

<<< @/src/sword-means-offer/64-sum-1-n-nums/index.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"husky": {
"hooks": {
"pre-commit": "npm run deploy && lint-staged",
"pre-commit": "npm run changelog && npm run deploy && git add . && lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
Expand Down
6 changes: 4 additions & 2 deletions plop-templates/code/README.md.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### 解题思路 or 实现原理
## 题目描述

### 参考
## 解题思路 or 实现原理

## 参考
5 changes: 3 additions & 2 deletions plop-templates/docs/zh/README.md.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 题目描述

## 解题思路 or 实现原理

## 实现代码

<<< @/{{path}}/{{name}}/index.ts
<<< @/{{templatePath}}/{{name}}/index.ts

## 参考

3 changes: 1 addition & 2 deletions src/sword-means-offer/03-find-repeat-number/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 题目
## 题目描述

找出数组中重复的数字。

Expand All @@ -12,7 +12,6 @@
[2, 3, 1, 0, 2, 5, 3]
输出: 2 或 3
```


限制:

Expand Down
27 changes: 27 additions & 0 deletions src/sword-means-offer/64-sum-1-n-nums/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### 题目描述

求 `1 + 2 + ... + n` , 要求 **不能使用** `乘除法`, `for`, `while`, `if`, `else`, `switch`, `case` 等关键字及条件判断语句 `(A ? B : C)` 。


示例 1:

```
输入: n = 3
输出: 6
```

示例 2:
```
输入: n = 9
输出: 45
```

限制:

1 <= n <= 10000

来源: [力扣 (LeetCode)](https://leetcode-cn.com/problems/qiu-12n-lcof)

## 解题思路 or 实现原理

我们小学就学过 `1 + 2 + ... + n` 的结果就是 $ sum = \frac{(1 + n) * n}{2} $, 但是由于题目限定, 我们不能使用常规方法, 所以我们可以通过 **短路递归** 和 **幂运算移位** 来计算出结果。
16 changes: 16 additions & 0 deletions src/sword-means-offer/64-sum-1-n-nums/__tests__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* @Author: Rainy
* @Date: 2020-02-06 19:05:18
* @LastEditors: Rainy
* @LastEditTime: 2020-07-28 15:46:52
*/

import { sumNums_1, sumNums_2 } from '.';

test('Test 64-sum-1-n-nums', () => {
expect(sumNums_1(3)).toBe(6);
});

test('Test 64-sum-1-n-nums', () => {
expect(sumNums_2(3)).toBe(6);
});
20 changes: 20 additions & 0 deletions src/sword-means-offer/64-sum-1-n-nums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @Author: Rainy
* @Date: 2019-11-14 19:25:01
* @LastEditors: Rainy
* @LastEditTime: 2020-07-28 15:42:46
*/

/**
* @param {number} n
* @return {number}
*/
export function sumNums_1(n: number): number {
return n && (n + sumNums_1(--n));
};

export function sumNums_2(n: number): number {
// (n + 1) * n / 2 => (n ^ 2 + n) / 2
return (n ** 2 + n) >> 1;
};