面试遇到的一个特无聊的问题--- 要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba" 第一种:使用字符串切片 result = s[::-1] 第二种:使用列表的reverse方法 l = list(s) l.reverse() result = "".join(l) 当然下面也行 l = list(s) result = "".join(l[::-1]) 第三种:使用…
按单词反转字符串是一道很常见的面试题.在Python中实现起来非常简单. def reverse_string_by_word(s): lst = s.split() # split by blank space by default return ' '.join(lst[::-1]) s = 'Power of Love' print reverse_string_by_word(s) # Love of Power s = 'Hello World!' print reverse_stri…
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit…