Skip to content

Commit 0f084df

Browse files
committed
更新文档和示例
1 parent 8b2c77a commit 0f084df

28 files changed

+844
-297
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
![img](https://raw.githubusercontent.com/dunwu/images/dev/snap/20200702071922.png)
2424

2525
- [算法概述](docs/overview.md)
26+
- [复杂度分析](docs/复杂度分析.md)
2627
- [数组和链表](docs/数组和链表.md)
2728
- [栈和队列](docs/栈和队列.md)
2829
- [线性表的查找](docs/线性表的查找.md)
2930
- [线性表的排序](docs/线性表的排序.md)
3031
- [跳表](docs/跳表.md)
31-
- [散列表](docs/散列表.md)
32+
- [哈希表](docs/哈希表.md)
3233
- [树和二叉树](docs/树和二叉树.md)
3334
- [](docs/堆.md)
3435
- [B+树](docs/B+树.md)

assets/散列表.eddx

16.7 KB
Binary file not shown.

assets/数据结构和算法.xmind

9.46 KB
Binary file not shown.

assets/线性结构.eddx

-27.9 KB
Binary file not shown.

assets/线性结构/单链表.eddx

68.3 KB
Binary file not shown.

assets/线性结构/双链表.eddx

75.9 KB
Binary file not shown.

assets/线性结构/数组.eddx

43.7 KB
Binary file not shown.
21.7 KB
Binary file not shown.

codes/algorithm/pom.xml

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<parent>
7-
<groupId>io.github.dunwu</groupId>
8-
<artifactId>dunwu-parent</artifactId>
9-
<version>0.5.6</version>
10-
</parent>
6+
<parent>
7+
<groupId>io.github.dunwu</groupId>
8+
<artifactId>dunwu-parent</artifactId>
9+
<version>1.0.8</version>
10+
</parent>
1111

12-
<groupId>io.github.dunwu.algorithm</groupId>
13-
<artifactId>algorithm</artifactId>
14-
<packaging>jar</packaging>
15-
<name>算法示例</name>
16-
<description>数据示例源码</description>
12+
<groupId>io.github.dunwu.algorithm</groupId>
13+
<artifactId>algorithm</artifactId>
14+
<packaging>jar</packaging>
15+
<name>算法示例</name>
16+
<description>数据示例源码</description>
1717

18-
<properties>
19-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20-
<java.version>1.8</java.version>
21-
<maven.compiler.source>${java.version}</maven.compiler.source>
22-
<maven.compiler.target>${java.version}</maven.compiler.target>
23-
</properties>
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<java.version>1.8</java.version>
21+
<maven.compiler.source>${java.version}</maven.compiler.source>
22+
<maven.compiler.target>${java.version}</maven.compiler.target>
23+
</properties>
2424

25-
<dependencies>
26-
<dependency>
27-
<groupId>io.github.dunwu</groupId>
28-
<artifactId>dunwu-tool-core</artifactId>
29-
</dependency>
30-
<dependency>
31-
<groupId>ch.qos.logback</groupId>
32-
<artifactId>logback-classic</artifactId>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.junit.jupiter</groupId>
36-
<artifactId>junit-jupiter</artifactId>
37-
</dependency>
38-
<dependency>
39-
<groupId>org.assertj</groupId>
40-
<artifactId>assertj-core</artifactId>
41-
</dependency>
42-
</dependencies>
25+
<dependencies>
26+
<dependency>
27+
<groupId>cn.hutool</groupId>
28+
<artifactId>hutool-all</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>ch.qos.logback</groupId>
32+
<artifactId>logback-classic</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.assertj</groupId>
40+
<artifactId>assertj-core</artifactId>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-javadoc-plugin</artifactId>
49+
<configuration>
50+
<doclint>none</doclint>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
4355
</project>

codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/寻找数组的中心索引.java

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1-
package io.github.dunwu.algorithm.array;
2-
3-
// 【寻找数组的中心索引】
1+
// 724. 寻找数组的中心下标
2+
//
3+
// 给你一个整数数组nums ,请计算数组的 中心下标 。
4+
//
5+
// 数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。
6+
//
7+
// 如果中心下标位于数组最左端,那么左侧数之和视为 0 ,因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。
8+
//
9+
// 如果数组有多个中心下标,应该返回 最靠近左边 的那一个。如果数组不存在中心下标,返回 -1 。
410
//
5-
// 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法。
611
//
7-
// 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。
812
//
9-
// 如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。
13+
// 示例 1:
1014
//
11-
// 示例 1:
15+
// 输入:nums = [1, 7, 3, 6, 5, 6]
16+
// 输出:3
17+
// 解释:
18+
// 中心下标是 3 。
19+
// 左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,
20+
// 右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ,二者相等。
21+
// 示例 2:
1222
//
13-
// 输入:
14-
// nums = [1, 7, 3, 6, 5, 6]
15-
// 输出: 3
16-
// 解释:
17-
// 索引3 (nums[3] = 6) 的左侧数之和(1 + 7 + 3 = 11),与右侧数之和(5 + 6 = 11)相等。
18-
// 同时, 3 也是第一个符合要求的中心索引。
19-
// 示例 2:
23+
// 输入:nums = [1, 2, 3]
24+
// 输出:-1
25+
// 解释:
26+
// 数组中不存在满足此条件的中心下标。
27+
// 示例 3:
2028
//
21-
// 输入:
22-
// nums = [1, 2, 3]
23-
// 输出: -1
24-
// 解释:
25-
// 数组中不存在满足此条件的中心索引。
26-
// 说明:
29+
// 输入:nums = [2, 1, -1]
30+
// 输出:0
31+
// 解释:
32+
// 中心下标是 0 。
33+
// 左侧数之和 sum = 0 ,(下标 0 左侧不存在元素),
34+
// 右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。
2735
//
28-
// nums 的长度范围为 [0, 10000]。
29-
// 任何一个 nums[i] 将会是一个范围在 [-1000, 1000]的整数。
36+
// 提示:
37+
//
38+
// 1 <= nums.length <= 104
39+
// -1000 <= nums[i] <= 1000
40+
//
41+
// 链接:https://leetcode-cn.com/problems/find-pivot-index
42+
43+
package io.github.dunwu.algorithm.array;
3044

3145
import org.junit.jupiter.api.Assertions;
3246

3347
/**
48+
* 724. 寻找数组的中心索引
49+
*
3450
* @author Zhang Peng
3551
* @since 2018-11-04
3652
*/
@@ -39,26 +55,27 @@ public class 寻找数组的中心索引 {
3955
public static void main(String[] args) {
4056
Assertions.assertEquals(3, pivotIndex(new int[] { 1, 7, 3, 6, 5, 6 }));
4157
Assertions.assertEquals(-1, pivotIndex(new int[] { 1, 2, 3 }));
58+
Assertions.assertEquals(0, pivotIndex(new int[] { 2, 1, -1 }));
4259
}
4360

4461
public static int pivotIndex(int[] nums) {
45-
int result = 0;
46-
for (int i = 0; i < nums.length; i++) {
47-
int sum1 = 0;
48-
int sum2 = 0;
49-
for (int a = 0; a < result; a++) {
50-
sum1 += nums[a];
51-
}
62+
for (int pos = 0; pos < nums.length; pos++) {
5263

53-
for (int b = result + 1; b < nums.length; b++) {
54-
sum2 += nums[b];
64+
// pos 左侧所有元素累加
65+
int leftSum = 0;
66+
for (int left = 0; left < pos; left++) {
67+
leftSum += nums[left];
5568
}
5669

57-
if (sum1 == sum2) {
58-
return result;
70+
// pos 右侧所有元素累加
71+
int rightSum = 0;
72+
for (int right = nums.length - 1; right > pos; right--) {
73+
rightSum += nums[right];
5974
}
6075

61-
result++;
76+
if (leftSum == rightSum) {
77+
return pos;
78+
}
6279
}
6380
return -1;
6481
}

0 commit comments

Comments
 (0)