今日頭條---後臺開發筆試題
阿新 • • 發佈:2018-12-11
第一題:
簡化Unix風格的路徑:
例項:
輸入:/home/
輸出:/home
例項2:
輸入:/a/./b/../../c/
輸出:/c
def test(path): pathlist=path.split('/') catch=[] result='' for data in pathlist: if data not in ["",".",".."]: catch.append(data) if '..'==data and catch: catch.pop(-1) if catch==[]: return "/" for data in catch: result=result+"/"+data+"" return result if __name__=='__main__': path=raw_input('') result=test(path) print result
執行結果:
執行AC
第二題:
得出字串字首,來唯一標識該字串;
if __name__=='__main__': n=int(raw_input()) strlist=[] result=[] t=[] flag = 0 for i in range(n): catch=raw_input() strlist.append(catch) for i in range(0,len(strlist)): result=[] for j in range(0,len(strlist)): for y in range(0,len(strlist[i])+1): if i!=j: item=strlist[i][:y] if strlist[j].find(item)!=0 : result.append(strlist[i][:y]) result.sort() break print result.pop() # # # 5 # bytedance # toutiaohao # toutiaoapp # iesaweme # iestiktok # b # toutiaoh # toutiaoa # iesa # iest # #
執行結果為:
結果:
執行20%;執行時間超時。。。。。。。。
歡迎大佬指點!!!!!!!!!!!!!