所以我想要做的是找到交替數字的數量,使它交替與-ve和正號 例如:1 -2 3 -4會讓我4 3 2 1從1到-4包括兩個數字,有4個數字。 Simillarly for 1 1 -3 2會讓我1 3 2 1 現在我有代碼,但我無法優化它,並且它會返回超出時間限制的錯誤,即使它適用於中等輸入流。使用for循環而不是while循環爲避免你的一些變量賦值如何減少循環次數或複雜度
j=0
count=0
length=(raw_input())
st=map(int,raw_input().split())
while j+1 < len(st):
k=j+1
count=0
temp=j
while k<len(st) and ((st[k]<0 and st[j]>0) or (st[k]>0 and st[j]<0)):
count+=1
k+=1
j+=1
print count+1,
j=temp+1
print 1
你的問題是題外話。但你可以試試[代碼評論](https://codereview.stackexchange.com/) –
如果你想獲得不同數字的計數,那麼只需將所有內容添加到一組並獲得設置長度 –
這屬於https ://codereview.stackexchange.com – Torxed