作者SecondRun (南爹摳打)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Wed Jun 5 15:16:28 2024
1002. Find Common Characters
找共通字串
Example 1:
Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
想法: 沒有,就硬幹
C# code:
public class Solution
{
public IList<string> CommonChars(string[] words)
{
var count = new int[26];
for (int i = 0; i < count.Length; i++)
{
count[i] = words[0].Count(x => x == 'a' + i);
for (int j = 1; j < words.Length; j++)
{
if (count[i] == 0) break;
count[i] = Math.Min(count[i], words[j].Count(x => x == 'a' + i));
}
}
var result = new List<string>();
for (int i=0; i<count.Length; i++)
{
while (count[i] != 0)
{
result.Add(((char)('a' + i)).ToString());
count[i]--;
}
}
return result;
}
}
--
(づ′・ω・)づ
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.220.51.52 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717571791.A.D43.html
推 JIWP: 大師 06/05 15:17
→ CanIndulgeMe: 沒力氣了,別再卷了…… 06/05 15:17
推 seanpai: 大師 06/05 15:19
推 DJYOSHITAKA: 別捲了... 06/05 15:24
推 sustainer123: 大師 06/05 15:24
推 deatheo: 大師 06/05 16:17
推 orangeNoob: 別捲了 06/05 16:27