https://leetcode.com/problems/reverse-string/
Reverse String - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문자열을 뒤집는 함수를 작성하라, 입력값은 문자 배열이며, 리턴 없이 리스트 내부를 직접 조작하라.
먼저 투 포인터(Two pointer)를 이용한 전통적인 방식으로 풀이해보자.
def reverseString_Twopointer(s):
left,right=0,len(s)-1
while left<right:
s[left],s[right]=s[right],s[left]
left+=1
right-=1
return s
다음은 파이썬 스타일이다.
def reverseString_pythonicway(s):
s.reverse()
reverse()는 리스트에만 제공된다. 만약 입력값이 문자열이라면 문자열 슬라이싱을 사용할 수 있다. 슬라이싱은 리스트에도 사용할 수 있으며, 성능 또한 매우 좋다.
'알고리즘' 카테고리의 다른 글
6. 문자열 관리(re 모듈을 통한 전처리) (0) | 2021.08.16 |
---|---|
5. 문자열 관리(특정한 기준으로 정렬하기) (0) | 2021.08.16 |
3. 문자열 - 유효한 펠린드롬 (0) | 2021.08.16 |
2. 딕셔너리의 주요 연산 시간 복잡도 및 특징 (0) | 2021.08.16 |
1. list의 주요 연산 시간복잡도 (0) | 2021.08.16 |