Skip to content

Commit 7c141ca

Browse files
committed
reformat codes
1 parent d0dd1fb commit 7c141ca

File tree

86 files changed

+770
-785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+770
-785
lines changed

.editorconfig

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
# EditorConfig helps developers define and maintain consistent
2-
# coding styles between different editors and IDEs
3-
# http://editorconfig.org
4-
# 所有文件换行使用 Unix like 风格(LF),bat 文件使用 win 风格(CRLF)
5-
# 缩进 java 4 个空格,其他所有文件 2 个空格
1+
# EditorConfig 用于在 IDE 中检查代码的基本 Code Style
2+
# @see: https://editorconfig.org/
3+
4+
# 配置说明:
5+
# 所有文件换行使用 Unix 风格(LF),*.bat 文件使用 Windows 风格(CRLF)
6+
# java / sh 文件缩进 4 个空格,其他所有文件缩进 2 个空格
67

78
root = true
89

910
[*]
10-
# Unix-style newlines with a newline ending every file
1111
end_of_line = lf
12-
13-
# Change these settings to your own preference
1412
indent_size = 2
15-
indent_style = space
13+
indent_style = tab
1614
max_line_length = 120
17-
18-
# We recommend you to keep these unchanged
1915
charset = utf-8
2016
trim_trailing_whitespace = true
2117
insert_final_newline = true
2218

23-
[*.bat]
19+
[*.{bat, cmd}]
2420
end_of_line = crlf
2521

26-
[*.java]
22+
[*.{java, groovy, kt, sh}]
2723
indent_size = 4
2824

2925
[*.md]

codes/algorithm/pom.xml

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

7-
<groupId>io.github.dunwu</groupId>
8-
<artifactId>algorithm-demos</artifactId>
9-
<version>1.0.1</version>
10-
<packaging>jar</packaging>
11-
<name>Algorithm Demos</name>
12-
<description>算法</description>
7+
<groupId>io.github.dunwu</groupId>
8+
<artifactId>algorithm-demos</artifactId>
9+
<version>1.0.1</version>
10+
<packaging>jar</packaging>
11+
<name>Algorithm Demos</name>
12+
<description>算法</description>
1313

14-
<properties>
15-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<java.version>1.8</java.version>
17-
<maven.compiler.source>${java.version}</maven.compiler.source>
18-
<maven.compiler.target>${java.version}</maven.compiler.target>
19-
</properties>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<java.version>1.8</java.version>
17+
<maven.compiler.source>${java.version}</maven.compiler.source>
18+
<maven.compiler.target>${java.version}</maven.compiler.target>
19+
</properties>
2020

21-
<dependencies>
22-
<dependency>
23-
<groupId>ch.qos.logback</groupId>
24-
<artifactId>logback-classic</artifactId>
25-
<version>1.1.3</version>
26-
</dependency>
27-
<dependency>
28-
<groupId>junit</groupId>
29-
<artifactId>junit</artifactId>
30-
<version>4.12</version>
31-
<scope>test</scope>
32-
</dependency>
33-
</dependencies>
21+
<dependencies>
22+
<dependency>
23+
<groupId>ch.qos.logback</groupId>
24+
<artifactId>logback-classic</artifactId>
25+
<version>1.1.3</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>4.12</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
3434
</project>

codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/TwoSum.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
//return [0, 1].
1313
public class TwoSum {
1414

15+
public static void main(String[] args) {
16+
int[] nums = new int[] {2, 7, 11, 15}
17+
int target = 18;
18+
int[] result = twoSum(nums, target);
19+
System.out.println(result);
20+
}
21+
1522
public static int[] twoSum(int[] nums, int target) {
1623
int[] result = new int[2];
1724
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
@@ -26,11 +33,4 @@ public static int[] twoSum(int[] nums, int target) {
2633
return result;
2734
}
2835

29-
public static void main(String[] args) {
30-
int[] nums = new int[] { 2, 7, 11, 15 };
31-
int target = 18;
32-
int[] result = twoSum(nums, target);
33-
System.out.println(result);
34-
}
35-
3636
}

codes/algorithm/src/main/java/io/github/dunwu/algorithm/hashtable/SubdomainVisitCount.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public class SubdomainVisitCount {
5050
public static void main(String[] args) {
5151
SubdomainVisitCount tmpl = new SubdomainVisitCount();
5252

53-
String[] s1 = new String[] { "9001 discuss.leetcode.com" };
54-
String[] s2 = new String[] { "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org" };
53+
String[] s1 = new String[] {"9001 discuss.leetcode.com"}
54+
String[] s2 = new String[] {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}
5555
tmpl.subdomainVisits(s1);
5656
tmpl.subdomainVisits(s2);
5757
}
@@ -69,10 +69,9 @@ public List<String> subdomainVisits(String[] cpdomains) {
6969
resultStringBuilder.append(domain);
7070
while (true) {
7171
map.put(resultStringBuilder.toString(),
72-
map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks);
72+
map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks);
7373
int dotPosition = resultStringBuilder.indexOf(".");
74-
if (dotPosition == -1)
75-
break;
74+
if (dotPosition == -1) { break; }
7675
resultStringBuilder.delete(0, dotPosition + 1);
7776
}
7877
}

codes/algorithm/src/main/java/io/github/dunwu/algorithm/hashtable/ToLowerCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public String toLowerCase(String str) {
3434
for (int i = 0; i < str.length(); i++) {
3535
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
3636
sb.append((char) (str.charAt(i) + 32));
37-
}
38-
else {
37+
} else {
3938
sb.append(str.charAt(i));
4039
}
4140
}

codes/algorithm/src/main/java/io/github/dunwu/algorithm/search/HashDemo.java

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class HashDemo {
3434
private final static int FAILED = 0xFFFFFFFF;
3535

3636
public static void main(String[] args) {
37-
// int[] list = {3, 112, 245, 27, 44, 19, 76, 29, 90};
38-
int[] list = { 1, 9, 25, 11, 12, 35, 17, 29 };
37+
// int[] list = {3, 112, 245, 27, 44, 19, 76, 29, 90}
38+
int[] list = {1, 9, 25, 11, 12, 35, 17, 29}
3939
HashTable[] ha = new HashTable[MAXSIZE];
4040
for (int i = 0; i < ha.length; i++) {
4141
ha[i] = new HashTable();
@@ -44,47 +44,56 @@ public static void main(String[] args) {
4444
HashDemo search = new HashDemo();
4545
search.createHashTable(ha, list, MODULO, MAXSIZE);
4646
search.displayHashTable(ha);
47-
4847
}
4948

5049
/**
51-
* 查找哈希表 构造哈希表采用除留取余法,即f(key) = key mod p (p ≤ size) 解决冲突采用开放定址法,即f2(key) = (f(key) +
52-
* i) mod p (1 ≤ i ≤ size-1) ha为哈希表,p为模,size为哈希表大小,key为要查找的关键字
50+
* 创建哈希表 先将哈希表中各关键字清空,使其地址为开放的,然后调用插入算法将给定的关键字序列依次插入。
5351
*/
54-
private int searchHashTable(HashTable[] ha, int p, int size, int key) {
55-
// 采用除留取余法找哈希地址
56-
int addr = key % p;
52+
public void createHashTable(HashTable[] ha, int[] list, int p, int size) {
53+
int i = 0;
5754

58-
// 若发生冲突,用开放定址法找下一个哈希地址
59-
while (ha[addr].key != NULLKEY && ha[addr].key != key) {
60-
addr = (addr + 1) % size;
55+
// 将哈希表中的所有关键字清空
56+
for (i = 0; i < ha.length; i++) {
57+
ha[i].key = NULLKEY;
58+
ha[i].count = 0;
6159
}
6260

63-
if (ha[addr].key == key) {
64-
// 查找成功
65-
return addr;
66-
}
67-
else {
68-
// 查找失败
69-
return FAILED;
61+
// 将关键字序列依次插入哈希表中
62+
for (i = 0; i < list.length; i++) {
63+
this.insertHashTable(ha, p, size, list[i]);
7064
}
7165
}
7266

7367
/**
74-
* 删除哈希表中关键字为key的记录 找到要删除的记录,将关键字置为删除标记DELKEY
68+
* 输出哈希表
7569
*/
76-
public int deleteHashTable(HashTable[] ha, int p, int size, int key) {
77-
int addr = 0;
78-
addr = searchHashTable(ha, p, size, key);
79-
if (FAILED != addr) {
80-
// 将该位置的关键字置为DELKEY
81-
ha[addr].key = DELKEY;
82-
return SUCCESS;
70+
private void displayHashTable(HashTable[] ha) {
71+
int i = 0;
72+
System.out.format("pos:\t", "pos");
73+
for (i = 0; i < ha.length; i++) {
74+
System.out.format("%4d", i);
8375
}
84-
else {
85-
// 查找不到记录,直接返回NULLKEY
86-
return NULLKEY;
76+
System.out.println();
77+
78+
System.out.format("key:\t");
79+
for (i = 0; i < ha.length; i++) {
80+
if (ha[i].key != NULLKEY) {
81+
System.out.format("%4d", ha[i].key);
82+
} else {
83+
System.out.format(" ");
84+
}
85+
}
86+
System.out.println();
87+
88+
System.out.format("count:\t");
89+
for (i = 0; i < ha.length; i++) {
90+
if (0 != ha[i].count) {
91+
System.out.format("%4d", ha[i].count);
92+
} else {
93+
System.out.format(" ");
94+
}
8795
}
96+
System.out.println();
8897
}
8998

9099
/**
@@ -100,8 +109,7 @@ private void insertHashTable(HashTable[] ha, int p, int size, int key) {
100109
ha[addr].key = key;
101110
ha[addr].count = 1;
102111
// 如果有冲突,使用开放定址法处理冲突
103-
}
104-
else {
112+
} else {
105113
do {
106114
// 寻找下一个哈希地址
107115
addr = (addr + 1) % size;
@@ -115,55 +123,41 @@ private void insertHashTable(HashTable[] ha, int p, int size, int key) {
115123
}
116124

117125
/**
118-
* 创建哈希表 先将哈希表中各关键字清空,使其地址为开放的,然后调用插入算法将给定的关键字序列依次插入。
126+
* 删除哈希表中关键字为key的记录 找到要删除的记录,将关键字置为删除标记DELKEY
119127
*/
120-
public void createHashTable(HashTable[] ha, int[] list, int p, int size) {
121-
int i = 0;
122-
123-
// 将哈希表中的所有关键字清空
124-
for (i = 0; i < ha.length; i++) {
125-
ha[i].key = NULLKEY;
126-
ha[i].count = 0;
127-
}
128-
129-
// 将关键字序列依次插入哈希表中
130-
for (i = 0; i < list.length; i++) {
131-
this.insertHashTable(ha, p, size, list[i]);
128+
public int deleteHashTable(HashTable[] ha, int p, int size, int key) {
129+
int addr = 0;
130+
addr = searchHashTable(ha, p, size, key);
131+
if (FAILED != addr) {
132+
// 将该位置的关键字置为DELKEY
133+
ha[addr].key = DELKEY;
134+
return SUCCESS;
135+
} else {
136+
// 查找不到记录,直接返回NULLKEY
137+
return NULLKEY;
132138
}
133139
}
134140

135141
/**
136-
* 输出哈希表
142+
* 查找哈希表 构造哈希表采用除留取余法,即f(key) = key mod p (p ≤ size) 解决冲突采用开放定址法,即f2(key) = (f(key) + i) mod p (1 ≤ i ≤ size-1)
143+
* ha为哈希表,p为模,size为哈希表大小,key为要查找的关键字
137144
*/
138-
private void displayHashTable(HashTable[] ha) {
139-
int i = 0;
140-
System.out.format("pos:\t", "pos");
141-
for (i = 0; i < ha.length; i++) {
142-
System.out.format("%4d", i);
143-
}
144-
System.out.println();
145+
private int searchHashTable(HashTable[] ha, int p, int size, int key) {
146+
// 采用除留取余法找哈希地址
147+
int addr = key % p;
145148

146-
System.out.format("key:\t");
147-
for (i = 0; i < ha.length; i++) {
148-
if (ha[i].key != NULLKEY) {
149-
System.out.format("%4d", ha[i].key);
150-
}
151-
else {
152-
System.out.format(" ");
153-
}
149+
// 若发生冲突,用开放定址法找下一个哈希地址
150+
while (ha[addr].key != NULLKEY && ha[addr].key != key) {
151+
addr = (addr + 1) % size;
154152
}
155-
System.out.println();
156153

157-
System.out.format("count:\t");
158-
for (i = 0; i < ha.length; i++) {
159-
if (0 != ha[i].count) {
160-
System.out.format("%4d", ha[i].count);
161-
}
162-
else {
163-
System.out.format(" ");
164-
}
154+
if (ha[addr].key == key) {
155+
// 查找成功
156+
return addr;
157+
} else {
158+
// 查找失败
159+
return FAILED;
165160
}
166-
System.out.println();
167161
}
168162

169163
}

codes/algorithm/src/main/java/io/github/dunwu/algorithm/search/Search.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface Search {
77

88
/**
99
* @param array 要查找的数组
10-
* @param key 要查找的 key
10+
* @param key 要查找的 key
1111
* @return 返回第一个匹配 key 值的数组元素所在位置
1212
*/
1313
<T extends Comparable<T>> int find(T array[], T key);

codes/algorithm/src/main/java/io/github/dunwu/algorithm/search/SearchStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class SearchStrategy {
1313

1414
private static final Logger logger = LoggerFactory.getLogger(SearchStrategy.class);
15+
1516
private Search search;
1617

1718
public SearchStrategy(Search search) {

codes/algorithm/src/main/java/io/github/dunwu/algorithm/search/strategy/BinarySearch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import io.github.dunwu.ds.search.Search;
44

55
/**
6-
* 二分查找又称折半查找,它是一种效率较高的查找方法。 二分查找需要两个前提:(1) 必须是顺序存储结构。(2) 必须是有序的表。 算法:
7-
* 从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字与给定值k相比较,若相等则表示查找成功; 若扫描结束仍没有找到等于关键字的结点,表示查找失败。 算法分析:
8-
* 最坏情况 O(log n) 最好情况 O(1) 平均情况 O(log n) 最坏情况下的空间复杂性 O(1)
6+
* 二分查找又称折半查找,它是一种效率较高的查找方法。 二分查找需要两个前提:(1) 必须是顺序存储结构。(2) 必须是有序的表。 算法: 从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字与给定值k相比较,若相等则表示查找成功;
7+
* 若扫描结束仍没有找到等于关键字的结点,表示查找失败。 算法分析: 最坏情况 O(log n) 最好情况 O(1) 平均情况 O(log n) 最坏情况下的空间复杂性 O(1)
98
*
109
* @author Zhang Peng
1110
*/
1211
public class BinarySearch implements Search {
1312

1413
/**
1514
* 查找方法
15+
*
1616
* @param array 被查找的线性表
17-
* @param key 被查找的 key
17+
* @param key 被查找的 key
1818
* @return 成功返回 key 的位置;失败返回 -1
1919
*/
2020
@Override

0 commit comments

Comments
 (0)