order 숫자 중 3, 6, 9의 개수만큼 반환해준다.
import Foundation
func solution(_ order:Int) -> Int {
var arr: [String] = []
for i in String(order) {
if i == "3" {
arr.append(String(i))
} else if i == "6" {
arr.append(String(i))
} else if i == "9" {
arr.append(String(i))
}
}
return arr.count
}
order 요소 중에 3, 6, 9가 있으면 빈 배열에 추가해주고 그 배열에 count를 반환해준다.
import Foundation
func solution(_ order:Int) -> Int {
return String(order).filter{$0 == "3" || $0 == "6" || $0 == "9"}.count
}
filter를 이용해서 요소 중에 3과 같거나 6과 같거나 9와 같으면 걸러내어 주고 count를 반환해준다.
import Foundation
func solution(_ order:Int) -> Int {
return String(order).filter { "369".contains($0) }.count
}
filter를 이용해서 요소 중에 3, 6, 9가 포함되어 있으면 걸러내어 주고 count를 반환해준다.
문자열.contains(포함되어있는지 알고싶은 문자) 👍