Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- pandas
- 코딩테스트
- 데이터프레임
- 부트캠프후기
- 유데미코리아
- 유데미큐레이션
- 정렬
- 유데미
- numpy
- 시각화
- 유데미부트캠프
- 그리디 알고리즘
- 브루트포스 알고리즘
- 판다스
- matplotlb
- 데이터시각화
- Leetcode
- Tableau
- 태블로
- 파이썬
- 백준
- 취업부트캠프
- DataFrame
- python
- 데이터드리븐
- 넘파이
- ndarray
- Til
- 데이터분석
- 스타터스부트캠프
Archives
- Today
- Total
Diary, Data, IT
[LeetCode] 344. Reverse String - Python 본문
[LeetCode] 344. Reverse String - Python
Reverse String - LeetCode
Can you solve this real interview question? Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algo
leetcode.com
문제
주어진 문자열을 반전시키는 함수를 작성해주세요. 입력 문자열은 배열 형태로 제공됩니다.
O(1)의 메모리를 사용해야 합니다.
아이디어
O(1)의 메모리만 사용해야하기 때문에 슬라이싱 등을 사용할 수 없다.
reverse 함수를 사용하거나, 투 포인터를 이용하여 위치를 하나씩 바꿔주는 과정을 거쳐야 한다.
구체적으로 투 포인터를 이용한 방법은, 좌측 포인터와 우측 포인터가 가운데로 한칸씩 이동하며
포인터가 위치한 자리의 값을 서로 바꿔주는 방식으로 진행한다.
코드
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
s.reverse()
코드 2
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
#투 포인터를 이용한 방법
left = 0; right = len(s) -1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1; right -= 1
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 200. Number of Islands - Python (0) | 2023.04.15 |
---|---|
[LeetCode] 3. Longest Substring Without Repeating Characters - Python (0) | 2023.04.14 |
[LeetCode] 937. Reorder Data in Log Files - Python (0) | 2023.04.11 |
[LeetCode] 819. Most Common Word - Python (0) | 2023.04.11 |
[LeetCode] 125. Valid Palindrome - Python (0) | 2023.04.10 |