File tree Expand file tree Collapse file tree 5 files changed +120
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +120
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ public boolean containsDuplicate (int [] nums ) {
4+ Arrays .sort (nums );
5+
6+ for (int i = 0 ; i < nums .length -1 ; i ++){
7+ if (nums [i ] == nums [i +1 ]) return true ;
8+ }
9+ return false ;
10+ }
11+ }
12+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ public int rob (int [] nums ) {
4+ int [] house = new int [nums .length ];
5+ Arrays .fill (house , -1 );
6+ return maxRobbery (0 , nums , house );
7+ }
8+
9+ private int maxRobbery (int index , int [] nums , int [] house ) {
10+ if (index >= nums .length ) return 0 ;
11+ if (house [index ] != -1 ) return house [index ];
12+
13+ int rob = nums [index ] + maxRobbery (index + 2 , nums , house );
14+ int skip = maxRobbery (index + 1 , nums , house );
15+
16+ house [index ] = Math .max (rob , skip );
17+ return house [index ];
18+ }
19+ }
20+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public int longestConsecutive (int [] nums ) {
5+ Set <Integer > checkList = new HashSet <>();
6+ int seqCnt = 0 ;
7+ int start = Integer .MIN_VALUE ;
8+
9+ for (int n : nums ){
10+ checkList .add (n );
11+ }
12+
13+ for (int n : nums ) {
14+ int seq = 1 ;
15+ int target = n +1 ;
16+ if (checkList .contains (n -1 ))continue ;
17+
18+ while (checkList .contains (target )){
19+ checkList .remove (target );
20+ seq ++;
21+ target ++;
22+ }
23+
24+ if (seqCnt < seq ){
25+ seqCnt = seq ;
26+ start = n ;
27+ }
28+ }
29+ return seqCnt ;
30+ }
31+ }
32+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public int [] topKFrequent (int [] nums , int k ) {
5+ Map <Integer ,Integer > counts = new HashMap <>();
6+ List <Integer > ordering = new ArrayList <>();
7+ int [] results = new int [k ];
8+
9+ for (int n : nums ){
10+ if (counts .containsKey (n )){
11+ counts .put (n , counts .get (n )+1 );
12+ continue ;
13+ }
14+ counts .put (n , 1 );
15+ ordering .add (n );
16+ }
17+
18+ ordering .sort ((o1 ,o2 ) -> counts .get (o2 ) - counts .get (o1 ));
19+ for (int i = 0 ; i < k ; i ++){
20+ results [i ] = ordering .get (i );
21+ }
22+
23+ return results ;
24+ }
25+ }
26+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ public int [] twoSum (int [] nums , int target ) {
4+ Map <Integer ,Integer > element = new HashMap <>();
5+
6+ for (int i = 0 ; i < nums .length ; i ++){
7+ element .put (i , nums [i ]);
8+ }
9+
10+ int [] result = new int [2 ];
11+ int n = 0 ;
12+ for (int i = 0 ; i < nums .length ; i ++){
13+ element .remove (i );
14+ n = target - nums [i ];
15+ if (element .containsValue (n )){
16+ result [0 ] = i ;
17+ break ;
18+ }
19+ }
20+
21+ for (int i = 0 ; i < nums .length ; i ++){
22+ if (nums [i ] == n && i != result [0 ]){
23+ result [1 ] = i ;
24+ break ;
25+ }
26+ }
27+ return result ;
28+ }
29+ }
30+
You can’t perform that action at this time.
0 commit comments