Skip to content

Commit b74f40e

Browse files
add 645
1 parent 05f0351 commit b74f40e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|645|[Set Mismatch](https://leetcode.com/problems/set-mismatch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_645.java) | O(nlogn) |O(1) | Easy |
2324
|644|[Maximum Average Subarray II](https://leetcode.com/problems/maximum-average-subarray-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_644.java) | |O(1) | Hard | Binary Search
2425
|643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_643.java) | O(n) |O(1) | Easy |
2526
|642|[Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_642.java) | O(n) |O(n) | Hard | Design
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 645. Set Mismatch
7+
*
8+
* The set S originally contains numbers from 1 to n.
9+
* But unfortunately, due to the data error, one of the numbers in
10+
* the set got duplicated to another number in the set,
11+
* which results in repetition of one number and loss of another number.
12+
13+
Given an array nums representing the data status of this set after the error.
14+
Your task is to firstly find the number occurs twice and then find the number
15+
that is missing. Return them in the form of an array.
16+
17+
Example 1:
18+
Input: nums = [1,2,2,4]
19+
Output: [2,3]
20+
21+
Note:
22+
The given array size will in the range [2, 10000].
23+
The given array's numbers won't have any order.
24+
*/
25+
public class _645 {
26+
public int[] findErrorNums(int[] nums) {
27+
int[] result = new int[2];
28+
Arrays.sort(nums);
29+
for (int i = 0; i < nums.length-1; i++) {
30+
if (nums[i+1] == nums[i]) {
31+
result[0] = nums[i];
32+
}
33+
}
34+
long temp = 0;
35+
for (int i = 0; i < nums.length; i++) {
36+
temp += i+1;
37+
temp -= nums[i];
38+
}
39+
temp += result[0];
40+
result[1] = (int) temp;
41+
return result;
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._645;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
/**
10+
* Created by stevesun on 7/23/17.
11+
*/
12+
public class _645Test {
13+
private static _645 test;
14+
private static int[] nums;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new _645();
19+
}
20+
21+
@Test
22+
public void test1(){
23+
nums = new int[]{1,2,2,4};
24+
assertArrayEquals(new int[]{2,3}, test.findErrorNums(nums));
25+
}
26+
}

0 commit comments

Comments
 (0)