[Programmers] 이어 붙인 수

2024. 8. 11. 21:21
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내 풀이

import Foundation

func solution(_ num_list:[Int]) -> Int {
    var odd: String = ""
    var even: String = ""
    
    for i in 0..<num_list.count{
        if(num_list[i] % 2 == 0){
            even.append(String(num_list[i]))
        }
        else{
            odd.append(String(num_list[i]))
        }
    }
    
    
    return Int(even)! + Int(odd)!
}

 

다른 사람의 풀이

import Foundation

func solution(_ numList: [Int]) -> Int {
    return Int(numList.filter { $0 % 2 != 0 }.map { String($0) }.joined())! + Int(numList.filter { $0 % 2 == 0 }.map { String($0) }.joined())!
}
728x90

BELATED ARTICLES

more