요구능력 : 문자열의 Index를 활용할 줄 아는지
코드설명 : 반복문으로 쓰기 위해 a~z를 ascii코드로 바꿔서 넣고 다시 unicode->String으로 바꿧다.
예를들어, 반복문으로 a를 ascii코드로 넣었으면 다시 a를 얻어서 range()로 a의 Index를 얻었다.
굳이 저렇게 해서 index를 얻은이유는 distance로 문자열에서 위치를 구하기위함이다.
후기 : ascii코드로 바꿔서 넣어야겠다! 까지는 생각했는데.. UnicodeScalar와 distance함수를 모르고 있엇어서 구현을 못했다..
막상해보니까 쉬운듯...
import Foundation
let s = String(readLine()!)
for i in Character("a").asciiValue!...Character("z").asciiValue! {
if let range = s.range(of:String(UnicodeScalar(i))) {
let s = s.distance(from: s.startIndex, to: range.lowerBound)
print("\(s)",terminator: " ")
}else{
print("-1", terminator: " ")
}
}
print("")
'Algorithm > 문제풀이_백준' 카테고리의 다른 글
Swift) 백준 1152번 (0) | 2021.07.28 |
---|---|
Swift) 백준 1157번 (0) | 2021.07.28 |
Swift) 백준 2675번 (0) | 2021.07.26 |
Swift) 백준 11720번 (0) | 2021.07.24 |
Swift) 백준 11654번(ASCII) (0) | 2021.07.21 |
댓글