Repository navigation
Expand file tree
/
Copy pathSorting.java
More file actions
85 lines (57 loc) · 1.51 KB
/
Copy pathSorting.java
File metadata and controls
85 lines (57 loc) · 1.51 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
LeetCode: 41. First Missing Positive
Difficulty: Hard
Topics: Arrays and Sorting
class Solution {
public int firstMissingPositive(int[] nums) {
int i = 0;
while (i < nums.length) {
int correct = nums[i] - 1;
// Only place positive numbers in the range [1, n]
if (nums[i] > 0 &&
nums[i] <= nums.length &&
nums[i] != nums[correct]) {
int temp = nums[i];
nums[i] = nums[correct];
nums[correct] = temp;
} else {
i++;
}
}
// Find the first missing positive
for (i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
// If all positions are correct
return nums.length + 1;
}
}
*/
/*
Leetcode: 645. Set Mismatch
Difficulty: Easy
Topics: Arrays and Sorting
class Solution {
public int[] findErrorNums(int[] nums) {
int i = 0;
while (i < nums.length) {
int correct = nums[i] - 1;
if (nums[i] != nums[correct]) {
int temp = nums[i];
nums[i] = nums[correct];
nums[correct] = temp;
} else {
i++;
}
}
for (i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
return new int[]{nums[i], i + 1};
}
}
return new int[]{-1, -1};
}
}
*/