File tree 3 files changed +70
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
20
20
21
21
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
22
22
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
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 |
23
24
|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
24
25
| 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 |
25
26
|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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments