Created
February 22, 2016 06:01
-
-
Save zillwc/47f24438ee21eb7eb267 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FindDuplicateNumber { | |
| public static int summationRes(int[] array, int len) { | |
| for (int i = 0; i < len; i++) { | |
| array[array[i] % len] += len; | |
| if (array[array[i] % len] / len == 2) | |
| return array[i] % len; | |
| } | |
| return -999; | |
| } | |
| public static int cycleDetectAlgo(int[] array, int len) { | |
| int fast = len - 1; | |
| int slow = len - 1; | |
| while (true) { | |
| slow = array[slow]; | |
| fast = array[array[fast]]; | |
| if (slow == fast) | |
| break; | |
| } | |
| return array[slow]; | |
| } | |
| public static void main(String[] args) { | |
| int[] array = {1, 2, 3, 1, 4, 4}; | |
| System.out.println(cycleDetectAlgo(array, array.length)); // 4 | |
| System.out.println(summationRes(array, array.length)); // 1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment