Skip to content

Commit 51e135e

Browse files
committed
update
1 parent 0fbe1b2 commit 51e135e

File tree

3 files changed

+89
-107
lines changed

3 files changed

+89
-107
lines changed

src/main/java/g0101_0200/s0139_word_break/Solution.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,33 @@
55
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
66
// #2024_11_15_Time_1_ms_(99.42%)_Space_42.1_MB_(80.42%)
77

8-
import java.util.HashSet;
98
import java.util.List;
10-
import java.util.Set;
119

1210
public class Solution {
13-
Boolean[] memo;
11+
private Boolean[] memo;
12+
1413
public boolean wordBreak(String s, List<String> wordDict) {
1514
memo = new Boolean[s.length() + 1];
1615
return dp(s, 0, wordDict);
1716
}
1817

19-
public boolean dp(String s, int i, List<String> wordDict){
20-
if(i == s.length()) return true;
21-
if(memo[i] != null) return memo[i];
22-
for(String word: wordDict){
18+
public boolean dp(String s, int i, List<String> wordDict) {
19+
if (i == s.length()) {
20+
return true;
21+
}
22+
if (memo[i] != null) {
23+
return memo[i];
24+
}
25+
for (String word : wordDict) {
2326
int len = word.length();
24-
if(i + len > s.length()){
27+
if (i + len > s.length()) {
2528
continue;
2629
}
2730
String subStr = s.substring(i, i + len);
28-
if(!subStr.equals(word)){
31+
if (!subStr.equals(word)) {
2932
continue;
3033
}
31-
if(dp(s, i + len, wordDict)){
34+
if (dp(s, i + len, wordDict)) {
3235
memo[i] = true;
3336
return true;
3437
}

src/main/java/g0101_0200/s0152_maximum_product_subarray/Solution.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
public class Solution {
88
public int maxProduct(int[] nums) {
9-
int m=Integer.MIN_VALUE;
10-
int n=nums.length;
11-
int start=1;
12-
int end=1;
13-
for(int i=0;i<n;i++){
14-
if(start==0)
15-
start=1;
16-
if (end==0)
17-
end=1;
18-
start=start*nums[i];
19-
end=end*nums[n-i-1];
20-
m=Math.max(m,Math.max(start,end));
21-
9+
int m = Integer.MIN_VALUE;
10+
int n = nums.length;
11+
int start = 1;
12+
int end = 1;
13+
for (int i = 0; i < n; i++) {
14+
if (start == 0) {
15+
start = 1;
16+
}
17+
if (end == 0) {
18+
end = 1;
19+
}
20+
start = start * nums[i];
21+
end = end * nums[n - i - 1];
22+
m = Math.max(m, Math.max(start, end));
2223
}
2324
return m;
2425
}

src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java

Lines changed: 62 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,116 +5,102 @@
55
// #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N)
66
// #2024_11_15_Time_30_ms_(99.78%)_Space_55.1_MB_(72.51%)
77

8+
import java.util.Arrays;
9+
810
@SuppressWarnings("java:S1104")
911
public class Trie {
10-
boolean ans=false;
11-
TrieNode[] trees=new TrieNode[26];
12-
public Trie() {
13-
14-
}
12+
private boolean ans = false;
13+
private final TrieNode[] trees = new TrieNode[26];
14+
public Trie() {}
1515

16-
TrieNode mapWordToTree(TrieNode t, String word, int i){
17-
char m=word.charAt(i);
18-
boolean found=false;
19-
TrieNode a=t.nexts[m-'a'];
20-
if(a!=null){
21-
if(i!=word.length()-1){
22-
mapWordToTree(a,word,i+1);
23-
found=true;
24-
25-
}else{
26-
a.end=true;
27-
found=true;
16+
TrieNode mapWordToTree(TrieNode t, String word, int i) {
17+
char m = word.charAt(i);
18+
boolean found = false;
19+
TrieNode a = t.nexts[m - 'a'];
20+
if (a != null) {
21+
if (i != word.length() - 1) {
22+
mapWordToTree(a, word, i + 1);
23+
} else {
24+
a.end = true;
2825
}
26+
found = true;
2927
}
30-
if(!found){
31-
TrieNode prev=t;
32-
for(int j=i;j<word.length();j++){
33-
TrieNode temp=new TrieNode(word.charAt(j));
34-
prev.nexts[word.charAt(j)-'a']=temp;
35-
prev=temp;
28+
if (!found) {
29+
TrieNode prev = t;
30+
for (int j = i; j < word.length(); j++) {
31+
TrieNode temp = new TrieNode(word.charAt(j));
32+
prev.nexts[word.charAt(j) - 'a'] = temp;
33+
prev = temp;
3634
}
37-
prev.end=true;
35+
prev.end = true;
3836
}
3937
return t;
4038
}
4139

4240
public void insert(String word) {
43-
char a=word.charAt(0);
44-
if(trees[a-'a']==null){
45-
TrieNode t=new TrieNode(a);
46-
trees[a-'a']=t;
47-
if(1==word.length()){
48-
trees[a-'a'].end=true;
49-
return;
50-
}
51-
trees[a-'a']=mapWordToTree(trees[a-'a'],word,1);
52-
}else{
53-
if(1==word.length()){
54-
trees[a-'a'].end=true;
55-
return;
56-
}
57-
trees[a-'a']=mapWordToTree(trees[a-'a'],word,1);
41+
char a = word.charAt(0);
42+
if (trees[a - 'a'] == null) {
43+
TrieNode t = new TrieNode(a);
44+
trees[a - 'a'] = t;
45+
}
46+
if (1 == word.length()) {
47+
trees[a - 'a'].end = true;
48+
return;
5849
}
59-
//System.out.println(trees[a-'a']);
50+
trees[a - 'a'] = mapWordToTree(trees[a - 'a'], word, 1);
6051
}
61-
public boolean searchWordInTree(TrieNode t, String word, int i){
62-
char a=word.charAt(i);
63-
TrieNode m=t.nexts[a-'a'];
64-
if(m!=null){
6552

66-
if(i==word.length()-1){
67-
ans=true;
68-
if(m.end){
69-
return true;
70-
}else{
71-
return false;
72-
}
53+
public boolean searchWordInTree(TrieNode t, String word, int i) {
54+
char a = word.charAt(i);
55+
TrieNode m = t.nexts[a - 'a'];
56+
if (m != null) {
57+
if (i == word.length() - 1) {
58+
ans = true;
59+
return m.end;
7360
}
74-
return searchWordInTree(m,word, i+1);
75-
61+
return searchWordInTree(m, word, i + 1);
7662
}
7763
return false;
7864
}
65+
7966
public boolean search(String word) {
80-
char a=word.charAt(0);
81-
if(trees[a-'a']==null){
67+
char a = word.charAt(0);
68+
if (trees[a - 'a'] == null) {
8269
return false;
83-
}else{
84-
if(1==word.length()){
85-
if(trees[a-'a'].end){
86-
return true;
87-
}else{
88-
return false;
89-
}
70+
} else {
71+
if (1 == word.length()) {
72+
return trees[a - 'a'].end;
9073
}
91-
return searchWordInTree(trees[a-'a'],word,1);
74+
return searchWordInTree(trees[a - 'a'], word, 1);
9275
}
9376
}
9477

9578
public boolean startsWith(String prefix) {
96-
char a=prefix.charAt(0);
97-
ans=false;
98-
if(trees[a-'a']==null){
79+
char a = prefix.charAt(0);
80+
ans = false;
81+
if (trees[a - 'a'] == null) {
9982
return false;
100-
}else{
101-
if(1==prefix.length()){
83+
} else {
84+
if (1 == prefix.length()) {
10285
return true;
10386
}
104-
searchWordInTree(trees[a-'a'],prefix,1);
87+
searchWordInTree(trees[a - 'a'], prefix, 1);
10588
}
10689
return ans;
10790
}
10891

109-
class TrieNode{
92+
static class TrieNode {
11093
char val;
111-
boolean end=false;
112-
TrieNode[] nexts=new TrieNode[26];
113-
TrieNode(char val){
114-
this.val=val;
94+
boolean end = false;
95+
TrieNode[] nexts = new TrieNode[26];
96+
97+
TrieNode(char val) {
98+
this.val = val;
11599
}
116-
@Override public String toString(){
117-
return val+" "+nexts+" "+end;
100+
101+
@Override
102+
public String toString() {
103+
return val + " " + Arrays.toString(nexts) + " " + end;
118104
}
119105
}
120106
}
@@ -126,11 +112,3 @@ class TrieNode{
126112
* boolean param_2 = obj.search(word);
127113
* boolean param_3 = obj.startsWith(prefix);
128114
*/
129-
130-
/*
131-
* Your Trie object will be instantiated and called as such:
132-
* Trie obj = new Trie();
133-
* obj.insert(word);
134-
* boolean param_2 = obj.search(word);
135-
* boolean param_3 = obj.startsWith(prefix);
136-
*/

0 commit comments

Comments
 (0)