Archive/Questions

[programmers/Swift] 삼총사

Marco 2022. 11. 12. 15:13
728x90

programmers.co.kr - 코딩테스트연습 - Lv.1 - 연습문제 - 삼총사

 

프로그래머스

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

programmers.co.kr

Constraints :

  • 3 ≤ number의 길이 ≤ 13
  • -1,000 ≤ number의 각 원소 ≤ 1,000
  • 서로 다른 학생의 정수 번호가 같을 수 있습니다.

Solution.swift :

//
//  Created by Yongwoo Marco on 2022/11/12.
//  Copyright © 2022 Yongwoo Marco Kim. All rights reserved.
//

func solution(_ number:[Int]) -> Int {
	var result = 0
	
	for first in 0..<number.count - 2 {
		for second in (first+1)..<number.count {
			for third in (second+1)..<number.count {
				if number[first] + number[second] + number[third] == 0 {
					result += 1
				}
			}
		}
	}
	
	return result
}

print(solution([-2, 3, 0, 2, -5]))		 	// 2
print(solution([-3, -2, -1, 0, 1, 2, 3]))	// 5
print(solution([-1, 1, -1, 1]))				// 0

How I tried this :
곂치지 않는 인덱스를 순회하는 형태로 구현

 

문제에 관한 모든 저작권 : https://programmers.co.kr/

728x90