Skip to content

Commit c542032

Browse files
committed
🍱 leetcode 解题
1 parent 3150c0d commit c542032

File tree

4 files changed

+179
-1
lines changed

4 files changed

+179
-1
lines changed

codes/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<!-- PROPERTIES BEGIN -->
4444
<properties>
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46-
<java.version>1.6</java.version>
46+
<java.version>1.8</java.version>
4747
<maven.compiler.source>${java.version}</maven.compiler.source>
4848
<maven.compiler.target>${java.version}</maven.compiler.target>
4949
</properties>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.dunwu.algorithm.hashtable;
2+
3+
import java.util.HashSet;
4+
5+
/*
6+
https://leetcode.com/problems/jewels-and-stones/
7+
8+
You're given strings J representing the types of stones that are jewels, and S representing the stones you have.
9+
Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
10+
11+
The letters in J are guaranteed distinct, and all characters in J and S are letters.
12+
Letters are case sensitive, so "a" is considered a different type of stone from "A".
13+
14+
Example 1:
15+
16+
Input: J = "aA", S = "aAAbbbb"
17+
Output: 3
18+
Example 2:
19+
20+
Input: J = "z", S = "ZZ"
21+
Output: 0
22+
Note:
23+
24+
S and J will consist of letters and have length at most 50.
25+
The characters in J are distinct.
26+
*/
27+
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+
}
52+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.github.dunwu.algorithm.hashtable;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/*
9+
https://leetcode.com/problems/subdomain-visit-count/
10+
11+
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the
12+
next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com".
13+
When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and
14+
"com" implicitly.
15+
16+
Now, call a "count-paired domain" to be a count (representing the number of visits this domain received),
17+
followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
18+
19+
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format
20+
as the input, and in any order), that explicitly counts the number of visits to each subdomain.
21+
22+
Example 1:
23+
Input:
24+
["9001 discuss.leetcode.com"]
25+
Output:
26+
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
27+
Explanation:
28+
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com"
29+
will also be visited. So they will all be visited 9001 times.
30+
31+
Example 2:
32+
Input:
33+
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
34+
Output:
35+
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
36+
Explanation:
37+
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times.
38+
For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
39+
40+
Notes:
41+
42+
The length of cpdomains will not exceed 100.
43+
The length of each domain name will not exceed 100.
44+
Each address will have either 1 or 2 "." characters.
45+
The input count in any count-paired domain will not exceed 10000.
46+
The answer output can be returned in any order.
47+
*/
48+
public class SubdomainVisitCount {
49+
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+
}
84+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.dunwu.algorithm.hashtable;
2+
3+
/*
4+
https://leetcode.com/problems/to-lower-case/
5+
6+
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
7+
8+
9+
10+
Example 1:
11+
12+
Input: "Hello"
13+
Output: "hello"
14+
Example 2:
15+
16+
Input: "here"
17+
Output: "here"
18+
Example 3:
19+
20+
Input: "LOVELY"
21+
Output: "lovely"
22+
23+
*/
24+
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+
}
42+
}

0 commit comments

Comments
 (0)