Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ecb33a6

Browse filesBrowse files
author
王俊超
committed
commit
1 parent 535e983 commit ecb33a6
Copy full SHA for ecb33a6

File tree

Expand file treeCollapse file tree

3 files changed

+61
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+61
-0
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 14:02
4+
**/
5+
public class Solution {
6+
public int missingNumber(int[] nums) {
7+
8+
if (nums == null || nums.length < 1) {
9+
throw new IllegalArgumentException("array should contain at least one element");
10+
}
11+
12+
for (int i = 0; i < nums.length; i++) {
13+
// 只会存在一个大于nums.length的数,将其放在数组最后一个位置
14+
if (nums[i] > nums.length) {
15+
swap(nums, i, nums.length - 1);
16+
}
17+
18+
// 交换位置,直到nums[i] = i
19+
while (nums[i] != i && nums[i] < nums.length) {
20+
swap(nums, i, nums[i]);
21+
}
22+
}
23+
24+
// 找出丢失的元素
25+
for (int i = 0; i < nums.length; i++) {
26+
if (nums[i] != i) {
27+
return i;
28+
}
29+
}
30+
31+
return nums.length;
32+
}
33+
34+
private void swap(int[] nums, int x, int y) {
35+
int temp = nums[x];
36+
nums[x] = nums[y];
37+
nums[y] = temp;
38+
}
39+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2018-10-10 14:14
6+
**/
7+
public class Test {
8+
public static void main(String[] args) {
9+
Solution solution = new Solution();
10+
11+
missingNumber(solution, new int[]{0});
12+
missingNumber(solution, new int[]{3, 0, 1});
13+
missingNumber(solution, new int[]{9, 6, 4, 2, 3, 5, 7, 0, 1});
14+
missingNumber(solution, new int[]{8, 6, 4, 2, 3, 5, 7, 0, 1});
15+
}
16+
17+
private static void missingNumber(Solution solution, int[] array) {
18+
System.out.println(solution.missingNumber(array));
19+
System.out.println(Arrays.toString(array));
20+
}
21+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.