Skip to content

Commit ab8ce3e

Browse files
committed
feat(algorithms): 15 3 sum
1 parent 069bee3 commit ab8ce3e

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.deploy_git
44
_book
55
node_modules
6+
.idea
67

78
## files
89
**/*.js

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# CONTRIBUTING
2+
3+
## Running
4+
5+
1. 创建 Problem 目录、index.ts
6+
2. tsc watch 实时编译
7+
3. 跑测试用例
8+
9+
# Booking
10+
11+
1. 初始化 README.md
12+
2. ts 代码转译 AST,获取注释信息
13+
3. 生成 readme.md 文档
14+
4. gitbook build
15+
16+
# Deploy
17+
18+
1. git push
19+
2. git push branch

algorithms/0015.3sum/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 3Sum
2+
3+
# Category Difficulty Likes Dislikes
4+
5+
algorithms Medium (23.92%) 3721 405
6+
Tags
7+
Companies
8+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
9+
10+
Note:
11+
12+
The solution set must not contain duplicate triplets.
13+
14+
Example:
15+
16+
Given array nums = [-1, 0, 1, 2, -1, -4],
17+
18+
A solution set is:
19+
[
20+
[-1, 0, 1],
21+
[-1, -1, 2]
22+
]

algorithms/0015.3sum/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 排列组合
3+
* 刚拿到这题第一个想法就是使用排列组合,很快写完。结果在遇到一个长3000的数组时,运行超时了,时间复杂度O(n!)。
4+
* n!/m!(n-m)! 差不多就是 44.95501亿次运算循环,这不管使用何种语言都会遇到超时的问题
5+
* 看了几个 Submission,他们的时间复杂度能做到 O(n^2)
6+
* @time 2019.05.20 18:00
7+
* @status Time Limit Exceeded
8+
* @case [82597,-9243,62390,83030,-97960,-26521...] (3000)
9+
*/
10+
var threeSum = (nums: number[]): number[][] => {
11+
const result: { [T: string]: number[] } = {};
12+
for (let a: number = 0; a < nums.length; a++) {
13+
for (let b: number = a + 1; b < nums.length; b++) {
14+
for (let c: number = b + 1; c < nums.length; c++) {
15+
if (nums[a] + nums[b] + nums[c] === 0) {
16+
const arr: number[] = [nums[a], nums[b], nums[c]].sort();
17+
const arrStr: string = arr.toString();
18+
if (!(arrStr in result)) {
19+
result[arrStr] = arr;
20+
}
21+
}
22+
}
23+
}
24+
}
25+
return Object.values(result);
26+
};
27+
28+
/**
29+
* 排列组合
30+
* @github https://github.com/guocaoyi/leetcode-ts
31+
* @author yalda
32+
*/
33+
var threeSum = (nums: number[]): number[][] => {
34+
const result: { [T: string]: number[] } = {};
35+
nums = nums.sort();
36+
let left = 0;
37+
let right = nums[nums.length - 1];
38+
39+
for (let a: number = 0; a < nums.length; a++) {
40+
for (let b: number = a + 1; b < nums.length; b++) {
41+
for (let c: number = b + 1; c < nums.length; c++) {
42+
if (nums[a] + nums[b] + nums[c] === 0) {
43+
const arr: number[] = [nums[a], nums[b], nums[c]].sort();
44+
const arrStr: string = arr.toString();
45+
if (!(arrStr in result)) {
46+
result[arrStr] = arr;
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
return Object.values(result);
54+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"scripts": {
1919
"clean": "",
2020
"create": "",
21-
"watch": "",
21+
"watch": "tsc -w ./algorithms/0015.3sum/index.ts",
2222
"doc": "",
2323
"index": "",
2424
"deploy": ""
@@ -44,4 +44,4 @@
4444
"branch": "gh-pages",
4545
"dir": "_book"
4646
}
47-
}
47+
}

script/compiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* watch & ts compiler
3+
*/

0 commit comments

Comments
 (0)