编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略网友思路参考 文章:Reverse a string in Python - Stack Overflow字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略耗费时间对比方法耗费时间T1、使用字符串切片的方法str01: 0.004107T2、使用reversed()方法str02: 0.007081T3、使用list和reverser()方法str03: 0.006830T4、使用reduce方法str04: 0.202377T5、while循环+空列表+字符串拼接str05: 0.027280T6、利用list和字符串拼接str06: 0.036437T7、while循环+字符串拼接str07: 0.036202T8、利用栈的思维str08: 0.057554T9、利用递归思想最长!T10、利用一行代码(for循环+字符串拼接)str10: 0.030771T11、利用for循环+倒叙提字母+字合并符串str11: 0.027477T1、使用字符串切片的方法import time# str_temp = '123456789'str_temp = '123456789'*10000# T1、使用字符串切片的方法STime1 = time.clock()str01 = str_temp[::-1]print(str01)ETime1 = time.clock()CostTime1=ETime1-STime1print('str01:','%.6f' % CostTime1) T2、使用reversed()方法# T2、使用reversed()方法STime2 = time.clock()str02 = ''.join(reversed(str_temp))print(str02)ETime2 = time.clock()CostTime2=ETime2-STime2print('str02:','%.6f' % CostTime2) T3、使用list和reverser()方法# T3、使用list和reverser()方法STime3 = time.clock()str_temp2list = list(str_temp)str_temp2list.reverse()str03 = ''.join(str_temp2list)print(str03)ETime3 = time.clock()CostTime3=ETime3-STime3print('str03:','%.6f' % CostTime3) T4、使用reduce方法# T4、使用reduce方法from functools import reduceSTime4 = time.clock()str04 = reduce(lambda x,y : y+x, str_temp)print(str04)ETime4 = time.clock()CostTime4=ETime4-STime4print('str04:','%.6f' % CostTime4) T5、while循环+空列表+字符串拼接# T5、for循环+空列表+字符串拼接STime5 = time.clock()str05 = []str_temp_len = len(str_temp)while str_temp_len: str_temp_len -= 1 str05.append(str_temp[str_temp_len])print(''.join(str05))ETime5 = time.clock()CostTime5=ETime5-STime5print('str05:','%.6f' % CostTime5) T6、利用list和字符串拼接# T6、利用list和字符串拼接STime6 = time.clock()str06 = ''str_temp_len = len(str_temp)for i in range(str_temp_len - 1, -1, -1): str06 += str_temp[i]print(str06)ETime6 = time.clock()CostTime6=ETime6-STime6print('str06:','%.6f' % CostTime6) T7、while循环+字符串拼接# T7、while循环+字符串拼接STime7 = time.clock()str07 = ''str_temp_len = len(str_temp)while str_temp_len: str_temp_len -= 1 str07 += str_temp[str_temp_len]print(str07)ETime7 = time.clock()CostTime7=ETime7-STime7print('str07:','%.6f' % CostTime7) T8、利用栈的思维# T8、利用栈的思维STime8 = time.clock()str_temp2list = list(str_temp) #模拟全部入栈str08 = ""while len(str_temp2list)>0: str08 += str_temp2list.pop() #模拟出栈print(str08)ETime8 = time.clock()CostTime8=ETime8-STime8print('str08:','%.6f' % CostTime8) T9、利用递归思想# T9、利用递归思想def r_string(str_temp): if len(str_temp) == 1: return str_temp return str_temp[-1] + r_string(str_temp[:-1])STime9 = time.clock()str09 = r_string(str_temp)print(str09)ETime9 = time.clock()CostTime9=ETime9-STime9print('str09:','%.6f' % CostTime9) T10、利用一行代码(for循环+字符串拼接)# T10、利用一行代码(for循环+字符串拼接)STime10 = time.clock()str10 = ''.join(str_temp[len(str_temp) - i - 1] for i in range(len(str_temp)))print(str10)ETime10 = time.clock()CostTime10=ETime10-STime10print('str10:','%.6f' % CostTime10) T11、利用for循环+倒叙提字母+字合并符串# T11、利用for循环+倒叙提字母+字合并符串STime11 = time.clock()str11=''for i in range(1,len(str_temp)+1): str11=str11+str_temp[-i]print(str11)ETime11 = time.clock()CostTime11=ETime11-STime11print('str11:','%.6f' % CostTime11)