编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
2023-09-14 09:04:45 时间
编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
目录
字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
网友思路参考 文章:Reverse a string in Python - Stack Overflow
字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
耗费时间对比
方法 | 耗费时间 |
T1、使用字符串切片的方法 | str01: 0.004107 |
T2、使用reversed()方法 | str02: 0.007081 |
T3、使用list和reverser()方法 | str03: 0.006830 |
T4、使用reduce方法 | str04: 0.202377 |
T5、while循环+空列表+字符串拼接 | str05: 0.027280 |
T6、利用list和字符串拼接 | str06: 0.036437 |
T7、while循环+字符串拼接 | str07: 0.036202 |
T8、利用栈的思维 | str08: 0.057554 |
T9、利用递归思想 | 最长! |
T10、利用一行代码(for循环+字符串拼接) | str10: 0.030771 |
T11、利用for循环+倒叙提字母+字合并符串 | str11: 0.027477 |
T1、使用字符串切片的方法
import time
# str_temp = '123456789'
str_temp = '123456789'*10000
# T1、使用字符串切片的方法
STime1 = time.clock()
str01 = str_temp[::-1]
print(str01)
ETime1 = time.clock()
CostTime1=ETime1-STime1
print('str01:','%.6f' % CostTime1)
T2、使用reversed()方法
# T2、使用reversed()方法
STime2 = time.clock()
str02 = ''.join(reversed(str_temp))
print(str02)
ETime2 = time.clock()
CostTime2=ETime2-STime2
print('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-STime3
print('str03:','%.6f' % CostTime3)
T4、使用reduce方法
# T4、使用reduce方法
from functools import reduce
STime4 = time.clock()
str04 = reduce(lambda x,y : y+x, str_temp)
print(str04)
ETime4 = time.clock()
CostTime4=ETime4-STime4
print('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-STime5
print('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-STime6
print('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-STime7
print('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-STime8
print('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-STime9
print('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-STime10
print('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-STime11
print('str11:','%.6f' % CostTime11)
相关文章
- 5分钟NLP-知识问答(KBQA)两种主流方法:基于语义解析和基于信息检索的方法介绍
- 亿级流量架构之分布式事务思路及方法
- JS改变this指向的三种方法 (转载非原创)
- 二、方法【黑马JavaSE笔记】
- 权限Linux改变用户文件权限的方法(linux改文件的用户)
- 夏季空调怎么开最省电?少开就是最省电的方法 很多人忽视了
- 判断Linux下多个条件判断的实现方法(linuxif多个条件)
- Oracle数据库教学:用最简单的方法学习(oracle教学)
- MSSQL技术:生成RSS信息的新方法(mssql生成rss)
- MySQL实现两字段拼接的新方法(mysql拼接两个字段)
- Js-$.extend扩展方法使方法参数更灵活
- 在SQLServer中使用CLR调用.NET方法实现思路
- 扩展js对象数组的OrderByAsc和OrderByDesc方法实现思路
- android获取照片的快照思路及实现方法
- Asp.Net无刷新文件上传并显示进度条的实现方法及思路
- JavaScript模拟类机制及私有变量的方法及思路
- .NET单点登陆的实现方法及思路
- sqlserver实现oracle的sequence方法
- 使用php方法curl抓取AJAX异步内容思路分析及代码分享