오늘도 개발

24. Reverse String 본문

자료구조 & 알고리즘/Leetcode

24. Reverse String

Sueeeeeee 2022. 10. 14. 23:36

문제

array s를 in-place로 뒤집으시오.

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

 

내가 해결한 방식

Two-pointer 방식. i는 앞에서부터, j는 뒤에서부터 시작해서 i, j 요소 교체.

def reverseString(s):
    i = 0
    j = len(s) - 1

    while i < j:
        s[i], s[j] = s[j], s[i]
        i += 1
        j -= 1

    return s

'자료구조 & 알고리즘 > Leetcode' 카테고리의 다른 글

26. Two Sum II - Input array is sorted  (1) 2022.10.15
25. Array Partition I  (1) 2022.10.14
23. Longest Common Prefix  (0) 2022.10.14
22. Find the Index of the First Occurrence in a String  (0) 2022.10.12
21. Add Binary  (0) 2022.10.11