如何寫乙個函式獲得乙個陣列中的第二大數?

時間 2021-05-09 16:03:33

1樓:Cyandev

intnth_largest

(int

*arr

,int

count

,intn)

}int

result

=tmp[n

-1];free

(tmp

);return

result;}

不知道算不算抖機靈,如果memmove我們認為時間複雜度為 O(1) 的話,這個演算法的時間複雜度和空間複雜度就都是 O(n) 了。

2樓:菜鳥-翡青

int find_second_max(const int* data,const int count)

else if (data[i] < maxVal && data[i] > smaxVal)

smaxVal = data[i]; }

return smaxVal;}

3樓:Clones

可以簡單地先找出最大的,然後去掉這乙個元素,在剩下的裡面找最大的。

另外乙個方法是所謂「錦標賽」方法。將元素兩兩分組,然後逐層淘汰。在所有被「冠軍」淘汰的元素裡找到最大即可。

這個時間複雜度課本上說是理論上最低的,但空間複雜度要多很多,因為需要針對所有的元素儲存被它們淘汰的元素。

4樓:默然

dim a as double

dim b as double

a=array(0)

b=array(1)

for i= 2 to n

if array(i)>a

b=a,a=array(i)

else

if array(i)<b

a=a,b=b

else

b=array(i)

next i

不懂程式,寫著玩玩

請教大家乙個問題,如何把乙個陣列中的相同元素挑選出來,組成另外的陣列或者list

MMMartt 函式 瞎寫 式 js const concat curr other v,vs v undefined concat curr curr 0 v?v,curr v curr v other,vs curr other const getResult arr concat arr so...

JavaScript 怎麼快速宣告乙個陣列,長度為 100,元素全是 0?

xgqfrms string array const arrpadStart 100,map item,i i 1100 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...

如何找出乙個陣列中兩個不相鄰元素的和的最大值

TiiWoo include 遍歷一遍,O n 的複雜度 include using namespace std const intINF 1e9 intn intarr 110 int ans,tmp,maxx INF intmain cout endl return0 Aperodry 先從陣列...