-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDupInArray51_1.java
More file actions
42 lines (41 loc) · 1.39 KB
/
FindDupInArray51_1.java
File metadata and controls
42 lines (41 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package offer;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* Given an array nums containing n + 1 integers
* where each integer is between 1 and n (inclusive),
* prove that at least one duplicate number must exist.
*
* Assume that there is only one duplicate number, find the duplicate one.
*/
//这一题同数组中重复数字 51
public class FindDupInArray51_1 {
public int findDuplicate(int[] nums) {
int duplicate = 0;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i = 0; i < nums.length; i++) {
if(!map.containsKey(nums[i])){
map.put(nums[i], 1);
}else{
duplicate = nums[i];
break;
}
}
return duplicate;
}
public static void main(String[] args) {
int[] arr = {1,2,4,5,3,2};
System.out.println(new FindDupInArray51_1().findDuplicate(arr));
/* String a = "[aaa]vvvv[cc]";
Pattern pp = Pattern.compile("\\[.*?\\]");
Matcher m = pp.matcher(a);
while(m.find()){
System.out.println(m.group());
System.out.println(m.start() + ":" + m.end() + " ");
}
System.out.print("\n=====\n");
//�滻ÿ���ʵ�����������
System.out.println(a.replaceAll("\\[.*?\\]", ""));*/
}
}