์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 344. reverse-string (Easy) 2023/4/27

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 4. 27. 19:18
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
344. reverse-string 76.8% Easy
 

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

 

๋ฌธ์ œ ์š”์•ฝ.

๋ฌธ์ž์—ด์„ ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ผ

์กฐ๊ฑด 1) ์ž…๋ ฅ์€ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง„๋‹ค

 

 

Step1. ๋ฐฐ์—ด ๋’ค์ง‘๊ธฐ -> list.reverse() ์ด์šฉ

 

๋ฆฌ์ŠคํŠธ์—๋งŒ ํ•ด๋‹น
์›๋ž˜ ์ž…๋ ฅ ๊ฐ’์„ ์—ญ์ˆœ์œผ๋กœ ๋ณ€๊ฒฝ, ๋ฐ˜ํ™˜๊ฐ’ X
list.reverse()
๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ์ŠคํŠธ๋ง, ๋”•์…”๋„ˆ๋ฆฌ์— ์‚ฌ์šฉ
์›ํ˜•์„ ๋ณด์กดํ•˜๊ณ , ๋ณ€๊ฒฝ๋œ ๊ฐ’์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜
๋”ฐ๋ผ์„œ ์•ž์— ์ž๋ฃŒํ˜•์„ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•จ
์›ํ•˜๋Š” ์ž๋ฃŒํ˜•(reversed(sequence))

 

 

๊ตฌํ˜„

class Solution(object):
    def reverseString(self, s):
        s.reverse()
        return s

 

728x90