Skip to content

Commit f5856ce

Browse files
committed
update
1 parent c220a52 commit f5856ce

File tree

133 files changed

+6247
-5172
lines changed

Some content is hidden

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

133 files changed

+6247
-5172
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ root = true
1010
[*]
1111
end_of_line = lf
1212
indent_size = 2
13-
indent_style = tab
13+
indent_style = space
1414
max_line_length = 120
1515
charset = utf-8
1616
trim_trailing_whitespace = true
@@ -19,7 +19,7 @@ insert_final_newline = true
1919
[*.{bat, cmd}]
2020
end_of_line = crlf
2121

22-
[*.{java, groovy, kt, sh}]
22+
[*.{java, gradle, groovy, kt, sh}]
2323
indent_size = 4
2424

2525
[*.md]

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ hs_err_pid*
2929

3030
# maven plugin temp files
3131
.flattened-pom.xml
32+
package-lock.json
3233

3334

3435
# ------------------------------- javascript -------------------------------
@@ -47,6 +48,7 @@ npm-debug.log*
4748
yarn-debug.log*
4849
yarn-error.log*
4950
bundle*.js
51+
book.pdf
5052

5153

5254
# ------------------------------- intellij -------------------------------

LICENSE

Lines changed: 427 additions & 21 deletions
Large diffs are not rendered by default.

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</artifactId>
9+
<version>1.0.1</version>
10+
<packaging>jar</packaging>
11+
<name>algorithm</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>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.github.dunwu.algorithm.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
9+
* <p>
10+
* 注意:答案中不可以包含重复的三元组。
11+
* <p>
12+
* 示例:给定数组 nums = [-1, 0, 1, 2, -1, -4],
13+
* <p>
14+
* 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]
15+
*
16+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
17+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/243/array-and-sorting/1020/">三数之和</a>
18+
* @since 2020-01-18
19+
*/
20+
public class ThreeSum {
21+
22+
public static List<List<Integer>> threeSum(int[] nums) {
23+
List<List<Integer>> list = new ArrayList<>();
24+
25+
if (nums == null || nums.length < 3) return list;
26+
27+
int len = nums.length;
28+
Arrays.sort(nums);
29+
30+
for (int i = 0; i < len; i++) {
31+
if (nums[i] > 0) break;
32+
33+
// 去重
34+
if (i > 0 && nums[i] == nums[i - 1]) continue;
35+
36+
int L = i + 1;
37+
int R = len - 1;
38+
while (L < R) {
39+
int sum = nums[i] + nums[L] + nums[R];
40+
if (sum == 0) {
41+
list.add(Arrays.asList(nums[i], nums[L], nums[R]));
42+
while (L < R && nums[L] == nums[L + 1]) L++;
43+
while (L < R && nums[R] == nums[R - 1]) R--;
44+
L++;
45+
R--;
46+
} else if (sum < 0) {
47+
L++;
48+
} else if (sum > 0) {
49+
R--;
50+
}
51+
}
52+
}
53+
54+
return list;
55+
}
56+
57+
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.dunwu.ds.array;
1+
package io.github.dunwu.algorithm.array;
22

33
import java.util.HashMap;
44
import java.util.Map;
@@ -12,25 +12,25 @@
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-
}
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+
}
2121

22-
public static int[] twoSum(int[] nums, int target) {
23-
int[] result = new int[2];
24-
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
25-
for (int i = 0; i < nums.length; i++) {
26-
if (map.containsKey(target - nums[i])) {
27-
result[1] = i;
28-
result[0] = map.get(target - nums[i]);
29-
return result;
30-
}
31-
map.put(nums[i], i);
32-
}
33-
return result;
34-
}
22+
public static int[] twoSum(int[] nums, int target) {
23+
int[] result = new int[2];
24+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
25+
for (int i = 0; i < nums.length; i++) {
26+
if (map.containsKey(target - nums[i])) {
27+
result[1] = i;
28+
result[0] = map.get(target - nums[i]);
29+
return result;
30+
}
31+
map.put(nums[i], i);
32+
}
33+
return result;
34+
}
3535

3636
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.dunwu.ds.hashtable;
1+
package io.github.dunwu.algorithm.hashtable;
22

33
import java.util.HashSet;
44

@@ -26,29 +26,29 @@
2626
*/
2727
public class JewelsAndStones {
2828

29-
public static void main(String[] args) {
30-
JewelsAndStones tmpl = new JewelsAndStones();
31-
32-
int result1 = tmpl.numJewelsInStones("aA", "aAAbbbb");
33-
System.out.println("result1 = [" + result1 + "]");
34-
35-
int result2 = tmpl.numJewelsInStones("z", "ZZ");
36-
System.out.println("result1 = [" + result2 + "]");
37-
}
38-
39-
public int numJewelsInStones(String J, String S) {
40-
HashSet set = new HashSet();
41-
for (int i = 0; i < J.length(); i++) {
42-
set.add(J.charAt(i));
43-
}
44-
45-
int count = 0;
46-
for (int i = 0; i < S.length(); i++) {
47-
if (set.contains(S.charAt(i))) {
48-
count++;
49-
}
50-
}
51-
return count;
52-
}
29+
public static void main(String[] args) {
30+
JewelsAndStones tmpl = new JewelsAndStones();
31+
32+
int result1 = tmpl.numJewelsInStones("aA", "aAAbbbb");
33+
System.out.println("result1 = [" + result1 + "]");
34+
35+
int result2 = tmpl.numJewelsInStones("z", "ZZ");
36+
System.out.println("result1 = [" + result2 + "]");
37+
}
38+
39+
public int numJewelsInStones(String J, String S) {
40+
HashSet set = new HashSet();
41+
for (int i = 0; i < J.length(); i++) {
42+
set.add(J.charAt(i));
43+
}
44+
45+
int count = 0;
46+
for (int i = 0; i < S.length(); i++) {
47+
if (set.contains(S.charAt(i))) {
48+
count++;
49+
}
50+
}
51+
return count;
52+
}
5353

5454
}

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.dunwu.ds.hashtable;
1+
package io.github.dunwu.algorithm.hashtable;
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
@@ -47,39 +47,39 @@
4747
*/
4848
public class SubdomainVisitCount {
4949

50-
public static void main(String[] args) {
51-
SubdomainVisitCount tmpl = new SubdomainVisitCount();
52-
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"}
55-
tmpl.subdomainVisits(s1);
56-
tmpl.subdomainVisits(s2);
57-
}
58-
59-
public List<String> subdomainVisits(String[] cpdomains) {
60-
List<String> result = new ArrayList<>();
61-
Map<String, Integer> map = new HashMap<>(); // key: subdomain, value: frequency
62-
StringBuilder resultStringBuilder = new StringBuilder();
63-
64-
for (String cpdomain : cpdomains) {
65-
int indexSpace = cpdomain.indexOf(' ');
66-
int numClicks = Integer.parseInt(cpdomain.substring(0, indexSpace));
67-
String domain = cpdomain.substring(indexSpace + 1);
68-
resultStringBuilder.setLength(0);
69-
resultStringBuilder.append(domain);
70-
while (true) {
71-
map.put(resultStringBuilder.toString(),
72-
map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks);
73-
int dotPosition = resultStringBuilder.indexOf(".");
74-
if (dotPosition == -1) { break; }
75-
resultStringBuilder.delete(0, dotPosition + 1);
76-
}
77-
}
78-
79-
for (String domain : map.keySet())
80-
result.add(map.get(domain) + " " + domain);
81-
82-
return result;
83-
}
50+
public static void main(String[] args) {
51+
SubdomainVisitCount tmpl = new SubdomainVisitCount();
52+
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" };
55+
tmpl.subdomainVisits(s1);
56+
tmpl.subdomainVisits(s2);
57+
}
58+
59+
public List<String> subdomainVisits(String[] cpdomains) {
60+
List<String> result = new ArrayList<>();
61+
Map<String, Integer> map = new HashMap<>(); // key: subdomain, value: frequency
62+
StringBuilder resultStringBuilder = new StringBuilder();
63+
64+
for (String cpdomain : cpdomains) {
65+
int indexSpace = cpdomain.indexOf(' ');
66+
int numClicks = Integer.parseInt(cpdomain.substring(0, indexSpace));
67+
String domain = cpdomain.substring(indexSpace + 1);
68+
resultStringBuilder.setLength(0);
69+
resultStringBuilder.append(domain);
70+
while (true) {
71+
map.put(resultStringBuilder.toString(),
72+
map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks);
73+
int dotPosition = resultStringBuilder.indexOf(".");
74+
if (dotPosition == -1) { break; }
75+
resultStringBuilder.delete(0, dotPosition + 1);
76+
}
77+
}
78+
79+
for (String domain : map.keySet())
80+
result.add(map.get(domain) + " " + domain);
81+
82+
return result;
83+
}
8484

8585
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.dunwu.ds.hashtable;
1+
package io.github.dunwu.algorithm.hashtable;
22

33
/*
44
https://leetcode.com/problems/to-lower-case/
@@ -23,22 +23,22 @@ Implement function ToLowerCase() that has a string parameter str, and returns th
2323
*/
2424
public class ToLowerCase {
2525

26-
public static void main(String[] args) {
27-
ToLowerCase tmpl = new ToLowerCase();
28-
String result = tmpl.toLowerCase("Hello");
29-
System.out.println("result = [" + result + "]");
30-
}
31-
32-
public String toLowerCase(String str) {
33-
StringBuilder sb = new StringBuilder();
34-
for (int i = 0; i < str.length(); i++) {
35-
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
36-
sb.append((char) (str.charAt(i) + 32));
37-
} else {
38-
sb.append(str.charAt(i));
39-
}
40-
}
41-
return sb.toString();
42-
}
26+
public static void main(String[] args) {
27+
ToLowerCase tmpl = new ToLowerCase();
28+
String result = tmpl.toLowerCase("Hello");
29+
System.out.println("result = [" + result + "]");
30+
}
31+
32+
public String toLowerCase(String str) {
33+
StringBuilder sb = new StringBuilder();
34+
for (int i = 0; i < str.length(); i++) {
35+
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
36+
sb.append((char) (str.charAt(i) + 32));
37+
} else {
38+
sb.append(str.charAt(i));
39+
}
40+
}
41+
return sb.toString();
42+
}
4343

4444
}

0 commit comments

Comments
 (0)