문자열 뒤집기
입력받은 문자열을 반대로 출력해보자👍
✅ 리스트를 마지막부터 새로운 리스트 안에 넣기
i_list = ["h","e","l","l","o"]
o_list = list()
while i_list:
o_list.append(i_list.pop())
print(o_list)
✅ 투 포인터 사용
i_list = ["h","e","l","l","o"]
left, right = 0, len(i_list)-1
while left < right:
i_list[left], i_list[right] = i_list[right], i_list[left]
left += 1
right -= 1
print(i_list)
✅ reverse() 사용
i_list = ["h","e","l","l","o"]
i_list.reverse()
print(i_list)
✅ 슬라이싱 사용
i_list = ["h","e","l","l","o"]
print(i_list[::-1])
728x90
'💡코딩테스트 > Leetcode' 카테고리의 다른 글
유효한 팰린드롬 | Valid Palindrome (0) | 2023.06.06 |
---|---|
[python|파이썬] 세 수의 합 / 세수의 합이 0이 되는 경우 (0) | 2023.03.15 |
[ LeetCode | python ] 11. Container With Most Water (0) | 2023.02.01 |