题目来源:
leetcode题目,网址:LCR 120. 寻找文件副本 - 力扣(LeetCode)
解题思路:
使用哈希集合判断是否有元素重复出现即可。
解题代码:
- class Solution {
- public int findRepeatDocument(int[] documents) {
- Set<Integer> set=new HashSet<>();
- for(int i=0;i<documents.length;i++){
- if(set.contains(documents[i])){
- return documents[i];
- }else{
- set.add(documents[i]);
- }
- }
- return -1;
- }
- }
总结:
使用计数排序的方法计数也行。
无官方题解。