算法——反转字符串

反转字符串

不要使用新的列表,在原列表本身进行字符串反转。

class Solution:
    def reverseString(self, s) -> None:
        '''
        Do not return anything, modify s in-place instead.
        '''
        for i in range(1, len(s)+1):
            s.append(s.pop(-i))
        print(s)
        
        
if __name__ == '__main__':
    s = 'hello'
    s = list(s)
    Solution().reverseString(s)
    

反向取值,向弹出s[-1]值再加入列表,再弹出s[-2]值再加入列表,以此类推...直到遍历完整个列表。

猜你喜欢

转载自www.cnblogs.com/noonjuan/p/10993292.html