단어의 개수가 가장 많은 인덱스를 고르는 문제이다 for문 연습하기 좋은 문제
스위프트
class Solution {
func mostWordsFound(_ sentences: [String]) -> Int {
var x = 0
for i in sentences{
x = max(x, numberOfWords(i))
}
return x
}
func numberOfWords(_ s: String) -> Int {
var w = 1
for i in s where i == " "{
w+=1
}
return w
}
}
파이썬
class Solution:
def mostWordsFound(self, sentences: List[str]) -> int:
x=0
for i in range(len(sentences)):
x=max(len(sentences[i].split()),x)
return x
'리트코드 풀이' 카테고리의 다른 글
Leetcode 1470 풀이(파이썬, 스위프트) (0) | 2022.02.05 |
---|---|
Leetcode 1108 풀이(파이썬, 스위프트) (0) | 2022.02.05 |
Leetcode 2011 풀이(파이썬, 스위프트) (0) | 2022.02.04 |
Leetcode 1672 풀이(파이썬, 스위프트) (0) | 2022.02.04 |
Leetcode 1480 풀이(파이썬, 스위프트) (0) | 2022.02.03 |