My Easy Java Solution


  • 0
    B
     public int lastRemaining(int n) {
         boolean bLeft = true;
    		int head = 1;
    		int step = 1;
    		while(head+step<=n){
    			if (bLeft) {
    				head+=step;
    			}else {
    				int mod = ((n-head)/step)%2;
    				if (mod==0) {
    					head+=step;
    				}
    			}
    			step*=2;
    			bLeft=!bLeft;
    		}
    		return head;
        }
    

Log in to reply
 

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