2024年4月6日 星期六

[leetcode] [eazy] Q1002

public class Q1002 {
public static void main(String[] args) {
String[] words = {"bella","label","roller"};
commonChars(words);
}
public static List<String> commonChars(String[] words) {
List<String>ans = new ArrayList<>();
int[] count = new int[26];
Arrays.fill(count, Integer.MAX_VALUE);
for(String str: words){
int []cnt = new int[26];
str.chars().forEach(c->++cnt[c-'a']);
for(int i= 0; i<26; i++){
count[i] = Math.min(cnt[i], count[i]);
}
}
for(char c='a'; c<='z';c++){
while(count[c-'a']-->0){
ans.add("" + c);
}
}
return ans;
}
}

1 Arrays.fill方法沒用過, 可以用來產生空陣列


2 統計文出現次數可以用int[]count = new int[26]]


3 str.chars().forEach(c->++cnt[c-'a'])

可以用來把cnt陣列中出現的文母次數做一個統計


4 for(char c=='a';c<='z';c++){

while(count[c-'a']-->0){

ans.add(""+c);

}

}

能將陣列中出現的字母, 在轉換放進List答案中


eazy這麼難...

 

沒有留言:

張貼留言

invalid packging for parent POM com.xxxxxx:1.0SNAPSHOT must be "pom" but is "jar"

 出現在專案上面冒紅線, 並有紅字invalid packing...... 在本專案的原因是, 這裡有母子專案 有在子專案pom檔中聲明parent <parent> <artifactId> heima-leadnews-test </a...