当前位置:首页 » 《资源分享》 » 正文

编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略_一个处女座的程序猿

24 人参与  2022年01月10日 11:09  分类 : 《资源分享》  评论

点击全文阅读


编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略

目录

字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略

耗费时间对比

T1、使用字符串切片的方法

T2、使用reversed()方法

T3、使用list和reverser()方法

T4、使用reduce方法

T5、while循环+空列表+字符串拼接

T6、利用list和字符串拼接

T7、while循环+字符串拼接

T8、利用栈的思维

T9、利用递归思想

T10、利用一行代码(for循环+字符串拼接)

T11、利用for循环+倒叙提字母+字合并符串


网友思路参考 文章: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)


点击全文阅读


本文链接:http://zhangshiyu.com/post/33022.html

字符串  拼接  利用  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1