File tree Expand file tree Collapse file tree 4 files changed +57
-17
lines changed Expand file tree Collapse file tree 4 files changed +57
-17
lines changed Original file line number Diff line number Diff line change
1
+ Time spent : 20 min(Self + see hint)
2
+
3
+ GIVEN: an array
4
+
5
+ RETURNS: duplicate num and missing num
6
+
7
+ EXAMPLES:
8
+
9
+ ```
10
+ Input: nums = [1,2,2,4]
11
+ Output: [2,3]
12
+ ```
13
+
14
+
15
+
16
+ Use the kind of vote progress. Make an integer flag to represent if the num has occured before. If flag is true, then the num is duplicate one.
Original file line number Diff line number Diff line change
1
+ // 9ms
2
+ class Solution {
3
+ public int [] findErrorNums (int [] nums ) {
4
+ int [] result = new int [2 ];
5
+ int [] numsFlag = new int [nums .length + 1 ];
6
+ for (int i = 0 ; i < nums .length ; i ++) {
7
+ if (numsFlag [nums [i ]] == -1 ) {
8
+ result [0 ] = nums [i ];
9
+ }
10
+ else {
11
+ numsFlag [nums [i ]] = -1 ;
12
+ }
13
+ }
14
+ for (int i = 1 ; i <= numsFlag .length ; i ++) {
15
+ if (numsFlag [i ] == 0 ) {
16
+ result [1 ] = i ;
17
+ break ;
18
+ }
19
+ }
20
+ return result ;
21
+ }
22
+ }
23
+
24
+ // 58ms
25
+ public class Solution {
26
+ public int [] findErrorNums (int [] nums ) {
27
+ Map <Integer , Integer > map = new HashMap <Integer , Integer >();
28
+ int n = nums .length ;
29
+ int duplicate = 0 ;
30
+ long sum = (n * (n + 1 )) / 2 ;
31
+ for (int i : nums ) {
32
+ map .put (i , map .getOrDefault (i , 0 ) + 1 );
33
+ if (map .get (i ) == 2 ) {
34
+ duplicate = i ;
35
+ }
36
+ sum = sum - i ;
37
+ }
38
+ return new int []{duplicate , (int )sum + duplicate };
39
+ }
40
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 104
104
| 100 | 438 | [ Find All Anagrams in a String] [ 438 ] | Java | Easy | 2017.7.21 | HashMap | |
105
105
| 101 | 599 | [ Minimum Index Sum of Two Lists] [ 599 ] | Java | Easy | 2017.7.21 | HashMap | |
106
106
| 102 | 205 | [ Isomorphic Strings] [ 205 ] | Java | Easy | 2017.7.23 | Hash Table | 2018.1.7 |
107
- | 103 | 645 | [ Set Mismatch] [ 645 ] | Java | Easy | 2017.7.23 | HashMap | |
107
+ | 103 | 645 | [ Set Mismatch] [ 645 ] | Java | Easy | 2017.7.23 | HashMap, Array | 2018.1.14 |
108
108
| 104 | 204 | [ Count Primes] [ 204 ] | Java | Easy | 2017.7.24 | HashMap, Math | 2018.1.7 |
109
109
| 105 | 290 | [ Word Pattern] [ 290 ] | Java | Easy | 2017.7.24 | HashTable | 2018.1.7 |
110
110
| 106 | 12 | [ Integer to Roman] [ 12 ] | Java | Medium | 2017.7.27 | Math, String | 2018.1.4 |
You can’t perform that action at this time.
0 commit comments