My O(n) C++ Solution, 16ms


  • 1
    O
    class Solution {
    	public:
    		bool canJump(vector<int>& nums) {
    			int front = 0;
    			for(int i = 0; i <= front; i++){
    				if(front >= nums.size() - 1) return true;
    				if(i == front && nums[front] == 0) return false;
    				if(front < i + nums[i]) front = i + nums[i];
    			}
    		}
    		
    };

  • 0
    K

    the second line in the false is almost unnecessary.
    it would just exit the for loop in the next iteration.

    But nice solution :)

    if(i == front && nums[front] == 0) return false;

Log in to reply
 

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