16 Line Java Simple Solution


  • -11
    G
    public class NumArray {
    	private int[] numArray= null;
        public NumArray(int[] nums) {
        	numArray = nums;
        }
        public int sumRange(int i, int j) {
        	int sum=0;
            for( int k=0;k<numArray.length;k++) {
            	if(k>=i && k<=j) {
            		sum += numArray[k];
            	}
            	else {
            		continue;
            	}
            }
            return sum;
        }
    }

  • 0
    T

    Personal feeling more like C


  • 0
    T

    The for loop doesn't always have to go from start to end. for(int k = i; k <= j; k++) means the same, except there are less iterations of for. Also, how did this get accepted again?


  • 3
    Z

    Very interestingly this code gets accepted and the code I wrote which is similar(should be faster since I do not go through the whole array) gets rejected for exceeding time limit


  • 0

    @TWiStErRob "Also, how did this get accepted again?"

    With luck? I just submitted it five times and it did get accepted the first and fourth time.


  • 0
    T

    Ah, I thought the input was fixed random, not different random for every submit.


  • 0
    T

    Exactly, I had the same. Maybe if you submit yours many times you get lucky (see Stefan's comment).


  • 0
    T

    sorry,I just watch that,my first submit did also not get acceptted!you can try again!


  • 0

    @TWiStErRob I'm pretty sure the input is the same every time. But the system load isn't (or whatever else is responsible for differing times of the same code for the same input).


Log in to reply
 

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