I have used a static ArrayList. It works in eclipse but fails here. Pls help me!!


  • 0
    T
    static ArrayList<Integer> al = new ArrayList<Integer>();
    	
    	public void addNum(int num) {
    	al.add(num);	
    	}
    	
    	public double findMedian() {
    		double n;
    		if(al.size() == 1) {
    			n = (double)al.get(0);
    			return n;
    		}
    		else if((al.size()%2) != 0) {
    			n = (double)al.get(al.size()/2);
    			return n;
    		}
    		else {
    			n = ((1.0)*al.get(al.size()/2-1) + (1.0)*al.get(al.size()/2))/2;
    			return n;
    		}
    	}

  • 0
    M

    It runs here, but your solution is wrong, therefore it will not produce correct result. The static array is not the problem, but the algorithm itself.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.