記錄一次耗時比較久的2種方法

2022-05-31 16:39:11 字數 4421 閱讀 3750

待分析:記憶體與複雜度

問題:

有n個人圍成一圈,順序排號。從第乙個人開始報數(從1到3報數),凡報到3的人退出圈子,問最後留下的是原來第幾號的那位。
第一種解法:

#在成為領導者之前,成功的全部就是自我成長

#有n個人圍成一圈,順序排號。從第乙個人開始報數(從1到3報數),凡報到3的人退出圈子,問最後留下的是原來第幾號的那位。

#1.2.3.4.5,6 |1,2,4,5|1,2,5|1,5

#1,2,3,4,5

#1,2,3,4

#解決思路提供了三種情況的遍歷,採用了遞迴的方式,但不得不說增加了很多額外變數的開銷

def one_px(lst):

length=len(lst)

if(length%3==0):

newlst = queue_n(lst)

elif (length%3==1):

newlst = queue_n1(lst)

else:

newlst = queue_n2(lst)

if len(newlst)>=3:

one_px(newlst) #遞迴

else:

print(newlst)

return newlst

def queue_n(lst):

l=len(lst)

for i in range(l,-1,-1):

if (i+1)%3==0:

lst.remove(lst[i])

print('n')

print(lst)

return lst

def queue_n2(lst):

l=len(lst)

for i in range(l-2,-1,-1):

if (i + 1) % 3 == 0:

lst.remove(lst[i])

l = len(lst)

temp1 = lst[l - 2]

temp2 = lst[l-1]

print(lst)

for i in range(l-3,-1,-1):

lst[i+2]=lst[i]

lst[0]=temp1

lst[1]=temp2

print('n2')

print(lst)

return lst

def queue_n1(lst):

l=len(lst)

for i in range(l,-1,-1):

if (i + 1) % 3 == 0:

lst.remove(lst[i])

print(lst)

l=len(lst)

temp=lst[l-1]

for i in range(l-2,-1,-1):

lst[i+1]=lst[i]

lst[0]=temp

print('n1')

print(lst)

return lst

if __name__=='__main__':
執行結果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]

[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34]

n1[34, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32]

[34, 1, 4, 5, 8, 10, 13, 14, 17, 19, 22, 23, 26, 28, 31, 32]

n2[31, 32, 34, 1, 4, 5, 8, 10, 13, 14, 17, 19, 22, 23, 26, 28]

[31, 32, 1, 4, 8, 10, 14, 17, 22, 23, 28]

n1[28, 31, 32, 1, 4, 8, 10, 14, 17, 22, 23]

[28, 31, 1, 4, 10, 14, 22, 23]

n2[22, 23, 28, 31, 1, 4, 10, 14]

[22, 23, 31, 1, 10, 14]

n2[10, 14, 22, 23, 31, 1]

n[10, 14, 23, 31]

[10, 14, 31]

n1[31, 10, 14]

n[31, 10]

[31, 10]

10

第二種解法:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 9, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 2, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 7, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[1, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 8, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 5, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]

[0, 0, 0, 4, 0, 0, 0, 0, 0, 0]

SQL Server遍歷表中記錄的2種方法

sql server遍歷表一般都要用到游標,sql server中可以很容易的用游標實現迴圈,實現sql server遍歷表中記錄。本文將介紹利用使用表變數和游標實現資料庫中表的遍歷。以下 中,塊之間的差異已經用灰色的背景標記。1 declare temp table23 45 id intiden...

記錄一次webpack2的專案架構

1 npm init y 生成package.json專案描述檔案。7 建立.babelrc檔案將預設寫入 preset 8 分別建立兩個資料夾 src資料夾和build資料夾,前者用於放源 後者用於放打包後的的 9 webpack.config.js的配置 const path require p...

記錄一次2個branch的merge操作

在提交 時,遇到如下需求 兩個專案放兩個分支,共用1份原始碼解析xml branch1有自己的一些xml branch2有自己的一些xml branch2解析xml方法有修改,已commit並push branch1也想按照同樣的方法修改xml解析,但xml還需要保持獨特性 檢出 切換到分支bran...