File tree Expand file tree Collapse file tree 5 files changed +123
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ class Solution {
5
+ public boolean containsDuplicate (int [] nums ) {
6
+ Set <Integer > numSet = new HashSet ();
7
+
8
+ for (int num : nums ) {
9
+ numSet .add (num );
10
+ }
11
+
12
+ return numSet .size () != nums .length ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int rob (int [] nums ) {
3
+ int [] sums = new int [nums .length ];
4
+
5
+ sums [0 ] = nums [0 ];
6
+
7
+ if (nums .length > 1 ) {
8
+ sums [1 ] = nums [1 ];
9
+ }
10
+
11
+ if (nums .length > 2 ) {
12
+ sums [2 ] = nums [0 ] + nums [2 ];
13
+ }
14
+
15
+ if (nums .length > 3 ) {
16
+ for (int i = 3 ; i < nums .length ; i ++) {
17
+ sums [i ] = Math .max (nums [i ] + sums [i - 2 ], nums [i ] + sums [i - 3 ]);
18
+ }
19
+ }
20
+
21
+ int max = 0 ;
22
+ for (int sum : sums ) {
23
+ max = Math .max (sum , max );
24
+ }
25
+
26
+ return max ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ class Solution {
5
+ public int longestConsecutive (int [] nums ) {
6
+ Set <Integer > numSet = new HashSet <>();
7
+
8
+ for (int num : nums ) {
9
+ numSet .add (num );
10
+ }
11
+
12
+ int longestSize = 0 ;
13
+
14
+ for (int num : numSet ) {
15
+ if (!numSet .contains (num - 1 )) {
16
+ int current = num ;
17
+ int count = 1 ;
18
+
19
+ while (numSet .contains (current + 1 )) {
20
+ count ++;
21
+ current ++;
22
+ }
23
+
24
+ longestSize = Math .max (count , longestSize );
25
+ }
26
+ }
27
+
28
+ return longestSize ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+ import java .util .Map .Entry ;
4
+ import java .util .PriorityQueue ;
5
+
6
+ class Solution {
7
+ public int [] topKFrequent (int [] nums , int k ) {
8
+ int [] answer = new int [k ];
9
+ Map <Integer , Integer > frequent = new HashMap <>();
10
+
11
+ for (int num : nums ) {
12
+ frequent .put (num , frequent .getOrDefault (num , 1 ) + 1 );
13
+ }
14
+
15
+ PriorityQueue <Entry <Integer , Integer >> pq = new PriorityQueue <>((a , b ) -> b .getValue ().compareTo (a .getValue ()));
16
+ pq .addAll (frequent .entrySet ());
17
+
18
+ for (int i = 0 ; i < k ; i ++) {
19
+ answer [i ] = pq .poll ().getKey ();
20
+ }
21
+
22
+ return answer ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isPalindrome (String s ) {
3
+ int start = 0 ;
4
+ int end = s .length () - 1 ;
5
+
6
+ while (start < end ) {
7
+ if (!Character .isLetterOrDigit (s .charAt (start ))) {
8
+ start ++;
9
+ continue ;
10
+ }
11
+
12
+ if (!Character .isLetterOrDigit (s .charAt (end ))){
13
+ end --;
14
+ continue ;
15
+ }
16
+
17
+ if (Character .toLowerCase (s .charAt (start )) != Character .toLowerCase (s .charAt (end ))) {
18
+ return false ;
19
+ }
20
+
21
+ start ++;
22
+ end --;
23
+ }
24
+
25
+ return true ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments