-
Notifications
You must be signed in to change notification settings - Fork 0
/
数组中重复的数字.java
33 lines (29 loc) · 904 Bytes
/
数组中重复的数字.java
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
package com.algorithm.demo.剑指Offer;
public class 数组中重复的数字 {
public static int[] nums = {2, 3, 1, 0, 2, 5, 3};
public static void main(String[] args) {
boolean result = duplicate(nums);
System.out.println("result=" + result);
}
public static boolean duplicate(int[] nums) {
if (nums.length <= 0) {
return false;
}
for (int i = 0; i < nums.length; ++i) {
if (nums[i] < 0 || nums[i] > nums.length - 1) {
return false;
}
}
for (int i = 0; i < nums.length; ++i) {
while (nums[i] != i) {
if (nums[i] == nums[nums[i]]) {
return true;
}
int temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
return false;
}
}