포스트

82. 머쓱이보다 키 큰 사람


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import Foundation

func solution(_ array:[Int], _ height:Int) -> Int {
    
    var answer : Int = 0
    
    for i in array.indices{
        if array[i] > height {
            answer += 1
        }
    }

    return answer
}


for문과 if문을 통해 문제를 해결하였다.

다른코드를 보니 filter로 하였다 다음번에 해봐야겠다.

또한 for if문을 섞어 for문에 where로 했다. 예전에 해본거 같은데 다시 기억해둬야겠다.

1
2
3
4
5
6
7
8
9
10
11
12
13
func solution(_ array: [Int], _ height: Int) -> Int { array.filter { $0 > height }.count }
//

import Foundation

func solution(_ array:[Int], _ height:Int) -> Int {
    var result = 0
    for h in array where h > height {
        result += 1
    }
    return result
}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.