Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80165bf

Browse files
authored
Merge pull request#644 from kciccolella/leetcode295
Create 295-Find-Median-from-Data-Stream.js
2 parentsb6f2db5 +7ecfb23 commit80165bf

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
varMedianFinder=function(){
3+
this.array=[];
4+
};
5+
6+
/**
7+
*@param {number} num
8+
*@return {void}
9+
*/
10+
MedianFinder.prototype.addNum=function(num){
11+
varlow=0;
12+
varhigh=this.array.length-1;
13+
14+
while(low<=high){
15+
varmid=Math.floor((high+low)/2);
16+
17+
if(this.array[mid]<num){
18+
low=mid+1;
19+
}else{
20+
high=mid-1;
21+
}
22+
}
23+
24+
this.array.splice(low,0,num);
25+
};
26+
27+
/**
28+
*@return {number}
29+
*/
30+
MedianFinder.prototype.findMedian=function(){
31+
if(this.array.length%2===0){
32+
varmid=this.array.length/2;
33+
return(this.array[mid]+this.array[mid-1])/2;
34+
}else{
35+
varmid=Math.floor(this.array.length/2);
36+
returnthis.array[mid];
37+
}
38+
};
39+
40+
/**
41+
* Your MedianFinder object will be instantiated and called as such:
42+
* var obj = new MedianFinder()
43+
* obj.addNum(num)
44+
* var param_2 = obj.findMedian()
45+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp