1 public class Solution { 2 3 public int duplicate (int[] numbers) { 4 if(numbers.length == 0) { 5 return -1; 6 } 7 for(int i = 0; i < numbers.length; i ++) { 8 for(int j = i + 1; j < numbers.length; j ++) { 9 if(numbers[i] == numbers[j]) { 10 return numbers[i]; 11 } 12 } 13 } 14 return -1; 15 } 16 }
1 public class Solution { 2 public int duplicate (int[] numbers) { 3 if(numbers.length == 0) { 4 return -1; 5 } 6 int[] record = new int[numbers.length]; 7 for(int i = 0; i < numbers.length; i ++) { 8 record[numbers[i]] ++; 9 if(record[numbers[i]] > 1) { 10 return numbers[i]; 11 } 12 } 13 return -1; 14 } 15 }
原文:https://www.cnblogs.com/StringBuilder/p/14766687.html