LeetCode 344: Reverse String

이원희·2021년 3월 4일
0

📝 PS

목록 보기
61/65
post-thumbnail

문제 풀이

간단한 문제지만 두가지 풀이법을 남긴다.
처음에는 전형적으로 index를 가지고 Array의 범위를 정하고, 임시로 저장할 변수 front를 선언한 뒤 swap했다.
두번째는 Swift의 swapAt()을 이용해 구현했다.

func reverseString(_ s: inout [Character]) {
    for index in 0..<s.count / 2 {
        let front = s[index]
        s[index] = s[s.count - 1 - index]
        s[s.count - 1 - index] = front
    }
}
func reverseString(_ s: inout [Character]) {
    for index in 0..<s.count / 2 {
        s.swapAt(index, s.count - 1 - index)
    }
}

LeetCode

0개의 댓글