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這麼難...

 

沒有留言:

張貼留言

題目: 瞬間有100-200萬筆資料量進來, 如何最快安全的接收?

如果想到快, 可能直覺就是Redis, 但是... 問:瞬間有100-200萬筆資料量進來, 如果存到redis, 這樣操作有沒有可能壓垮redis? 答: 在瞬間大量資料(如100-200萬筆)快速進入Redis,確實有可能導致Redis的性能瓶頸甚至崩潰,特別是在以下情況下:...