面试遇到的一个特无聊的问题--- 要求:在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…
code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self.next=None def recurse(head,newhead): #递归,head为原链表的头结点,newhead为反转后链表的头结点 if head is None: return if head.next is None: newhead=head else : newhead=recu…