포스트

71. 조건에 맞게 수열 변환하기 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import Foundation

func solution(_ arr:[Int]) -> [Int] {
    
    var answer : [Int] = []
    
    answer = arr
    
    for i in answer.indices {
        
        if answer[i] >= 50 && answer[i] % 2 == 0 {
            answer[i] = answer[i] / 2
        } else if answer[i] < 50 && answer[i] % 2 != 0 {
            answer[i] = answer[i] * 2
        } else {
            continue
        }
        
    }
    
    
    return answer
}

처음에 잘못이해하고 첫번 째 if문에 바로 else를 하여, 정답을 내려고했다

그랬더니 99에서 막혔다. 생각해보니 두 조건에 해당하지 않는 숫자는 그대로 내보내야 해서, continue로 넘겼더니 해결이 되었다.

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