Skip to content

Commit d0dd1fb

Browse files
committed
reformat codes
1 parent 6bb4f2d commit d0dd1fb

File tree

99 files changed

+5198
-4937
lines changed

Some content is hidden

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

99 files changed

+5198
-4937
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> :keyboard: 项目同步维护在 [github](https://github.com/dunwu/algorithm-tutorial) | [gitee](https://gitee.com/turnon/algorithm-tutorial)
44
>
5-
> :book: [电子书](https://dunwu.github.io/algorithm-tutorial/) | [电子书(国内)](http://turnon.gitee.io/algorithm-tutorial/)
5+
> 📖 [电子书](https://dunwu.github.io/algorithm-tutorial/) | [电子书(国内)](http://turnon.gitee.io/algorithm-tutorial/)
66
77
## 笔记
88

codes/algorithm/pom.xml

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

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,26 @@
1111
//Because nums[0] + nums[1] = 2 + 7 = 9,
1212
//return [0, 1].
1313
public class TwoSum {
14-
public static int[] twoSum(int[] nums, int target) {
15-
int[] result = new int[2];
16-
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
17-
for (int i = 0; i < nums.length; i++) {
18-
if (map.containsKey(target - nums[i])) {
19-
result[1] = i;
20-
result[0] = map.get(target - nums[i]);
21-
return result;
22-
}
23-
map.put(nums[i], i);
24-
}
25-
return result;
26-
}
2714

28-
public static void main(String[] args) {
29-
int[] nums = new int[]{2, 7, 11, 15};
30-
int target = 18;
31-
int[] result = twoSum(nums, target);
32-
System.out.println(result);
33-
}
15+
public static int[] twoSum(int[] nums, int target) {
16+
int[] result = new int[2];
17+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
18+
for (int i = 0; i < nums.length; i++) {
19+
if (map.containsKey(target - nums[i])) {
20+
result[1] = i;
21+
result[0] = map.get(target - nums[i]);
22+
return result;
23+
}
24+
map.put(nums[i], i);
25+
}
26+
return result;
27+
}
28+
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+
3436
}

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@
2525
The characters in J are distinct.
2626
*/
2727
public class JewelsAndStones {
28-
public int numJewelsInStones(String J, String S) {
29-
HashSet set = new HashSet();
30-
for (int i = 0; i < J.length(); i++) {
31-
set.add(J.charAt(i));
32-
}
33-
34-
int count = 0;
35-
for (int i = 0; i < S.length(); i++) {
36-
if (set.contains(S.charAt(i))) {
37-
count++;
38-
}
39-
}
40-
return count;
41-
}
42-
43-
public static void main(String[] args) {
44-
JewelsAndStones tmpl = new JewelsAndStones();
45-
46-
int result1 = tmpl.numJewelsInStones("aA", "aAAbbbb");
47-
System.out.println("result1 = [" + result1 + "]");
48-
49-
int result2 = tmpl.numJewelsInStones("z", "ZZ");
50-
System.out.println("result1 = [" + result2 + "]");
51-
}
28+
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+
}
53+
5254
}

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,40 @@
4747
*/
4848
public class SubdomainVisitCount {
4949

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

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@ Implement function ToLowerCase() that has a string parameter str, and returns th
2222
2323
*/
2424
public class ToLowerCase {
25-
public String toLowerCase(String str) {
26-
StringBuilder sb = new StringBuilder();
27-
for (int i = 0; i < str.length(); i++) {
28-
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
29-
sb.append((char) (str.charAt(i) + 32));
30-
} else {
31-
sb.append(str.charAt(i));
32-
}
33-
}
34-
return sb.toString();
35-
}
36-
37-
public static void main(String[] args) {
38-
ToLowerCase tmpl = new ToLowerCase();
39-
String result = tmpl.toLowerCase("Hello");
40-
System.out.println("result = [" + result + "]");
41-
}
25+
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+
}
38+
else {
39+
sb.append(str.charAt(i));
40+
}
41+
}
42+
return sb.toString();
43+
}
44+
4245
}

0 commit comments

Comments
 (0)