알고리즘/Programmers
[Swift_Programmes] 서울에서 김서방 찾기
YEN_
2023. 11. 22. 11:01
프로그래머스 문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/12919
풀이
func solution(_ seoul:[String]) -> String {
return String("김서방은 \(seoul.firstIndex(of: "Kim")!)에 있다")
}
중요 개념
- 배열의 요소를 사용해서 인덱스를 구할 수 있는 메서드 -> firstIndex(of:)
- firstIndex(of:) -> 배열의 앞부터 순회하며 체크
- lastIndex(of:) -> 배열의 뒤부터 순회하며 체크
https://developer.apple.com/documentation/swift/array/firstindex(of:)
firstIndex(of:) | Apple Developer Documentation
Returns the first index where the specified value appears in the collection.
developer.apple.com