15 lines easy understanding java solution with O(n) time


  • 0
    Z
    class Solution {
        public int findDuplicate(int[] nums) {
            for (int i = 0; i < nums.length; i++) {
    			int cur = nums[i];
    			if (cur < 0) {
    				cur = -cur;
    			}
    			if (nums[cur] < 0) {
    				return cur;
    			}
    			else {
    				nums[cur] *= -1;
    			}
    		}
    		
    		return 0;
        }
    }
    

Log in to reply
 

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