Skip to content

Commit 35ee87e

Browse files
committed
✨ feat: 添加数组数据结构
1 parent 321efa0 commit 35ee87e

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

DataStructure/Array/Array.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 数组
2+
3+
> 数组是一种按顺序存储数据的集合,元素可以随机存取,因为数组中每个元素都可以通过数组索引识别。
4+
5+
## 双指针
6+
7+
> ​ 一种在排序数组中使用的技巧,利用几个不同位置的指针,通过速度或方向的变化解决问题。
8+
9+
### 和为Sum的两个数字
10+
11+
> 此部分代码在 FindNumWithSum.ts 中
12+
13+
输入一个递增排序的数组和一个数字`sum`,在数组中查找两个数,使得他们的和正好是`sum`,如果有多对数字的和等于`sum`,输出两个数的乘积最小的。
14+
15+
```typescript
16+
/**
17+
* 在一个有序数组中查找和为sum的两个数字,如果有多个,则输出乘积最小的。
18+
* @param {number[]} array
19+
* @param {number} sum
20+
* @return {number[]}
21+
*/
22+
function FindNumWithSum(array: number[], sum: number): number[] {
23+
// 因为返回乘积最小的 即和相同,越分散乘积越小,则从两侧开始查找
24+
// 设置左索引
25+
let left = 0
26+
// 设置右索引
27+
let right = array.length - 1
28+
while(left < right) {
29+
let total = array[left] + array[right]
30+
if (total === sum) {
31+
return [array[left], array[right]]
32+
} else if (total < sum) {
33+
// 和小于目标值,左索引向右移动
34+
left++
35+
} else {
36+
// 和大于目标值,右索引向左移动
37+
right--
38+
}
39+
}
40+
return []
41+
}
42+
```
43+
44+
### 和为Sum的连续正数序列
45+
46+
> 此部分代码在 FindContinousSequence.ts 中
47+
48+
输入一个正数`S`,打印出所有和为S的连续正数序列。例如,输入`15`,会返回`[[1,2,3,4,5], [4,5,6], [7,8]]`
49+
50+
```typescript
51+
/**
52+
* 输入一个正数sum,打印出所有和为sum的连续正数序列。
53+
* @param {number} sum
54+
* @return {number[]}
55+
*/
56+
function FindContinousSequence(sum: number): number[] {
57+
// 存储结果的数组
58+
let result = []
59+
// 表示当前的子序列
60+
let child = [1,2]
61+
// 子序列的头元素
62+
let left = 1
63+
// 子序列的尾元素
64+
let right = 2
65+
// 子序列当前总和
66+
let currentSum = 3
67+
while (right < sum) {
68+
while (currentSum < sum && right < sum) {
69+
child.push(++right)
70+
currentSum += right
71+
}
72+
while (currentSum > sum && left < sum) {
73+
child.shift()
74+
currentSum -= left++
75+
}
76+
// 当前已经存在和为sum的连续正数序列
77+
if (currentSum === sum && child.length > 1) {
78+
result.push(child.slice())
79+
// 继续向后寻找
80+
child.push(++right)
81+
currentSum += right
82+
}
83+
}
84+
return result
85+
}
86+
```
87+
88+
89+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
/**
4+
* 输入一个正数sum,打印出所有和为sum的连续正数序列。
5+
* @param {number} sum
6+
* @return {number[]}
7+
*/
8+
function FindContinousSequence(sum) {
9+
// 存储结果的数组
10+
var result = [];
11+
// 表示当前的子序列
12+
var child = [1, 2];
13+
// 子序列的头元素
14+
var left = 1;
15+
// 子序列的尾元素
16+
var right = 2;
17+
// 子序列当前总和
18+
var currentSum = 3;
19+
while (right < sum) {
20+
while (currentSum < sum && right < sum) {
21+
child.push(++right);
22+
currentSum += right;
23+
}
24+
while (currentSum > sum && left < sum) {
25+
child.shift();
26+
currentSum -= left++;
27+
}
28+
// 当前已经存在和为sum的连续正数序列
29+
if (currentSum === sum && child.length > 1) {
30+
result.push(child.slice());
31+
// 继续向后寻找
32+
child.push(++right);
33+
currentSum += right;
34+
}
35+
}
36+
return result;
37+
}
38+
exports.FindContinousSequence = FindContinousSequence;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/**
3+
* 输入一个正数sum,打印出所有和为sum的连续正数序列。
4+
* @param {number} sum
5+
* @return {number[]}
6+
*/
7+
function FindContinousSequence(sum: number): number[] {
8+
// 存储结果的数组
9+
let result = []
10+
// 表示当前的子序列
11+
let child = [1,2]
12+
// 子序列的头元素
13+
let left = 1
14+
// 子序列的尾元素
15+
let right = 2
16+
// 子序列当前总和
17+
let currentSum = 3
18+
while (right < sum) {
19+
while (currentSum < sum && right < sum) {
20+
child.push(++right)
21+
currentSum += right
22+
}
23+
while (currentSum > sum && left < sum) {
24+
child.shift()
25+
currentSum -= left++
26+
}
27+
// 当前已经存在和为sum的连续正数序列
28+
if (currentSum === sum && child.length > 1) {
29+
result.push(child.slice())
30+
// 继续向后寻找
31+
child.push(++right)
32+
currentSum += right
33+
}
34+
}
35+
return result
36+
}
37+
38+
export {FindContinousSequence}

DataStructure/Array/FindNumWithSum.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
/**
4+
* 在一个有序数组中查找和为sum的两个数字,如果有多个,则输出乘积最小的。
5+
* @param {number[]} array
6+
* @param {number} sum
7+
* @return {number[]}
8+
*/
9+
function FindNumWithSum(array, sum) {
10+
// 因为返回乘积最小的 即和相同,越分散乘积越小,则从两侧开始查找
11+
// 设置左索引
12+
var left = 0;
13+
// 设置右索引
14+
var right = array.length - 1;
15+
while (left < right) {
16+
var total = array[left] + array[right];
17+
if (total === sum) {
18+
return [array[left], array[right]];
19+
}
20+
else if (total < sum) {
21+
// 和小于目标值,左索引向右移动
22+
left++;
23+
}
24+
else {
25+
// 和大于目标值,右索引向左移动
26+
right--;
27+
}
28+
}
29+
return [];
30+
}
31+
exports.FindNumWithSum = FindNumWithSum;

DataStructure/Array/FindNumWithSum.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 在一个有序数组中查找和为sum的两个数字,如果有多个,则输出乘积最小的。
3+
* @param {number[]} array
4+
* @param {number} sum
5+
* @return {number[]}
6+
*/
7+
function FindNumWithSum(array: number[], sum: number): number[] {
8+
// 因为返回乘积最小的 即和相同,越分散乘积越小,则从两侧开始查找
9+
// 设置左索引
10+
let left = 0
11+
// 设置右索引
12+
let right = array.length - 1
13+
while(left < right) {
14+
let total = array[left] + array[right]
15+
if (total === sum) {
16+
return [array[left], array[right]]
17+
} else if (total < sum) {
18+
// 和小于目标值,左索引向右移动
19+
left++
20+
} else {
21+
// 和大于目标值,右索引向左移动
22+
right--
23+
}
24+
}
25+
return []
26+
}
27+
28+
export {FindNumWithSum}

DataStructure/Test/Array.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {FindNumWithSum} from '../Array/FindNumWithSum'
2+
import {FindContinousSequence} from '../Array/FindContinousSequence'
3+
4+
test('FindNumWithSum', () => {
5+
expect(FindNumWithSum([1,2,3,4,5,6,7,8], 11)).toEqual([3,8])
6+
})
7+
8+
test('FindCountinousSequence', () => {
9+
expect(FindContinousSequence(15)).toEqual([[1,2,3,4,5], [4,5,6], [7,8]])
10+
})

0 commit comments

Comments
 (0)