日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/21 编程问答 76 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編程筆試(解析及代碼實現):字符串反轉(字符串逆序輸出)代碼實現十多種方法對比(解析思路及其耗費時間)詳細攻略

目錄

字符串反轉(字符串逆序輸出)代碼實現十多種方法對比(解析思路及其耗費時間)詳細攻略

耗費時間對比

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 -= 1str05.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 -= 1str07 += 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_tempreturn 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)

總結

以上是生活随笔為你收集整理的编程笔试(解析及代码实现):字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。