File tree Expand file tree Collapse file tree 3 files changed +18
-16
lines changed
Expand file tree Collapse file tree 3 files changed +18
-16
lines changed Original file line number Diff line number Diff line change @@ -11,16 +11,19 @@ public int singleNumber(int[] nums) {
1111}
1212
1313
14- //Solution2: using HashMap T: O(n) S: O(n)
14+ // Solution2: HashMap
15+ // T: O(n) S: O(n)
1516class Solution {
1617 public int singleNumber (int [] nums ) {
18+ if (nums == null || nums .length == 0 ) return -1 ;
1719 Map <Integer , Integer > map = new HashMap <>();
18- for (int num : nums ) {
19- if (map .containsKey (num )) map .put (num , 0 );
20+ for (int num : nums ) {
21+ if (map .containsKey (num )) map .put (num , 0 );
2022 else map .put (num , 1 );
2123 }
22- for (int num : nums )
23- if (map .get (num ) == 1 ) return num ;
24- return nums [0 ];
24+ for (int num : nums ) {
25+ if (map .get (num ) == 1 ) return num ;
26+ }
27+ return -1 ;
2528 }
2629}
Original file line number Diff line number Diff line change 1- //Solution1: HashSet T: O(n) S: O(n)
1+ // Solution1: HashSet
2+ // T: O(n) S: O(n)
23class Solution {
34 public List <String > findRepeatedDnaSequences (String s ) {
45 Set <String > set = new HashSet <>();
Original file line number Diff line number Diff line change 11class Solution {
22 public boolean isHappy (int n ) {
3- Set <Integer > set = new HashSet <Integer >();
4- int sum = 0 , tmp = 0 ;
5-
6- while (set .add (n )) {
7- sum = 0 ;
8- while (n > 0 ) {
9- tmp = n % 10 ;
3+ Set <Integer > set = new HashSet <>();
4+ while (set .add (n )) {
5+ int sum = 0 ;
6+ while (n != 0 ) {
7+ int tmp = n % 10 ;
108 sum += tmp * tmp ;
11- n /= 10 ;
9+ n = n / 10 ;
1210 }
13- if (sum == 1 ) return true ;
11+ if (sum == 1 ) return true ;
1412 n = sum ;
1513 }
1614 return false ;
You can’t perform that action at this time.
0 commit comments