Trulyawesome
[Swift] 문자열 내 마음대로 정렬하기 본문
1
2
3
4
5
6
7
8
|
func solution(_ strings:[String], _ n:Int) -> [String] {
return strings.sorted {
let a = $0[$0.index($0.startIndex, offsetBy: n)]
let b = $1[$1.index($1.startIndex, offsetBy: n)]
return first == second ? $0 < $1 : a < b
}
}
|
cs |
String의 index를 얻기위해 위와같이 공식문서에서 index함수를 찾아서 사용했습니다.
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[Swift] H-Index (0) | 2020.09.15 |
---|---|
swift 수박수박수박수박수박수?(Programmers) (0) | 2020.01.18 |
프로그래머스 K번째 수 (Swift) (0) | 2020.01.11 |
프로그래머스 나누어떨어지는 숫자배열 (Swift) (0) | 2020.01.11 |