Algorithms - Reverse a string

Reverse a string

Do not use the new list, string reversal in the original list itself.

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)
    

Value reverse to eject s [-1] was added value list, then the pop-s [-2] was added value list, and so on ... until a complete traversal list.

 

Guess you like

Origin www.cnblogs.com/noonjuan/p/10993292.html