Guest User

Untitled

a guest
Jan 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. public int findDuplicate(int[] nums) {
  2. if (nums.length > 1){
  3. int ptr1 = nums[0];
  4. int ptr2 = nums[nums[0]];
  5.  
  6. while (ptr1 != ptr2)
  7. {
  8. ptr1 = nums[ptr1];
  9. ptr2 = nums[nums[ptr2]];
  10. }
  11.  
  12. ptr2 = 0;
  13. while (ptr2 != ptr1)
  14. {
  15. ptr2 = nums[ptr2];
  16. ptr1 = nums[ptr1];
  17. }
  18. return ptr1;
  19. }
  20. return -1;
  21.  
  22. }
Add Comment
Please, Sign In to add comment