File tree 5 files changed +100
-0
lines changed
longest-consecutive-sequence
5 files changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean containsDuplicate (int [] nums ) {
3
+ HashSet <Integer > seen = new HashSet <>();
4
+ for (int num : nums ) {
5
+ if (!seen .add (num )) {
6
+ return true ;
7
+ }
8
+ }
9
+
10
+ return false ;
11
+
12
+
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
3
+ class Solution {
4
+ int size = 0 ;
5
+ int [] numArray ;
6
+ int [] dp ;
7
+
8
+ public int rob (int [] nums ) {
9
+ size = nums .length ;
10
+ dp = new int [size ];
11
+ // 배열의 모든 값을 -1로 변경
12
+ Arrays .fill (dp , -1 );
13
+ numArray = nums ;
14
+ return fun (0 );
15
+ }
16
+
17
+ private int fun (int idx ) {
18
+ if (idx >= size ) return 0 ;
19
+ if (dp [idx ] != -1 ) return dp [idx ];
20
+ dp [idx ] = 0 ; // check
21
+ dp [idx ] += Math .max (fun (idx + 2 ) + numArray [idx ], fun (idx + 1 ));
22
+ return dp [idx ];
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+
3
+ class Solution {
4
+ public int longestConsecutive (int [] nums ) {
5
+ HashSet <Integer > mySet = new HashSet <Integer >();
6
+
7
+ for (int num : nums ) {
8
+ mySet .add (num );
9
+ }
10
+
11
+ int result = 0 ;
12
+ for (int num : mySet ) {
13
+ int cnt = 1 ;
14
+ if (!mySet .contains (num - 1 )) {
15
+ while (mySet .contains (++num )) {
16
+ ++cnt ;
17
+ }
18
+ result = Math .max (cnt , result );
19
+ }
20
+ }
21
+ return result ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+ class Solution {
4
+ public static int [] topKFrequent (int [] nums , int k ) {
5
+ Map <Integer , Integer > myMap = new HashMap <>();
6
+ for (int num : nums ) {
7
+ myMap .put (num , myMap .getOrDefault (num , 0 ) + 1 );
8
+ }
9
+ return myMap .entrySet ()
10
+ .stream ()
11
+ .sorted ((v1 , v2 ) -> Integer .compare (v2 .getValue (),v1 .getValue ()))
12
+ .map (Map .Entry ::getKey )
13
+ .mapToInt (Integer ::intValue )
14
+ .toArray ();
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isPalindrome (String s ) {
3
+ StringBuilder str = new StringBuilder ();
4
+
5
+ for (int i = 0 ; i < s .length (); i ++) {
6
+ char c = s .charAt (i );
7
+ if (Character .isLetterOrDigit (c )) {
8
+ str .append (Character .toLowerCase (c ));
9
+ }
10
+ }
11
+
12
+ int left = 0 , right = str .length () - 1 ;
13
+ while (left < right ) {
14
+ if (str .charAt (left ) != str .charAt (right )) {
15
+ return false ;
16
+ }
17
+ left ++;
18
+ right --;
19
+ }
20
+
21
+ return true ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments