File tree Expand file tree Collapse file tree
longest-consecutive-sequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public int climbStairs (int n ) {
5+ int [] steps = new int [n +1 ];
6+ if (n == 1 ) return 1 ;
7+ if (n == 2 ) return 2 ;
8+ if (n == 3 ) return 3 ;
9+ steps [1 ] = 1 ; steps [2 ] = 2 ;//2, 1+1
10+ steps [3 ] = 3 ;
11+ for (int i = 4 ;i < n +1 ; i ++){
12+ steps [i ] = steps [i -1 ] + steps [i -2 ];
13+ }
14+ /*
15+ steps[4] =
16+ 1+1+1+1
17+ 1+2+1
18+ 2+2
19+ 2+1+1
20+ 1+1+2
21+ */
22+
23+ return steps [n ];
24+
25+
26+ }
27+ }
28+
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+ //int[] 를 정렬
6+ Arrays .sort (nums );
7+ //maxlength를 갱신하는 식으로
8+ int maxLength = 0 ;
9+ int curLength = 0 ;
10+ long prev = Long .MIN_VALUE ;
11+ long cur = Long .MIN_VALUE ;
12+
13+
14+ for (int i =0 ; i < nums .length ; i ++){
15+
16+ //공통
17+ prev = cur ;
18+ cur = nums [i ];
19+ System .out .println ("i: " + i );
20+
21+ //System.out.println("cur: "+ cur);
22+ //System.out.println("prev: "+ prev);
23+ if (curLength == 0 ) curLength ++;
24+
25+ if (cur == prev + 1 ){
26+ curLength ++;
27+ maxLength = Math .max (maxLength , curLength );
28+ //System.out.println("curLength : " + curLength);
29+ //System.out.println("maxLength : " + maxLength);
30+ }else {//다르면
31+ System .out .println ("여기" );
32+
33+
34+ if (cur == prev ){
35+ maxLength = Math .max (maxLength , curLength );
36+ }else {
37+ maxLength = Math .max (maxLength , curLength );
38+ curLength = 0 ;
39+ }
40+
41+ //System.out.println("curLength : " + curLength);
42+ //System.out.println("maxLength : " + maxLength);
43+ }
44+
45+
46+
47+
48+
49+
50+ }
51+
52+ return maxLength ;
53+
54+ }
55+ }
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+ HashMap <Integer , Integer > maps = new HashMap <>(); //nums[i] 대상, 등장 횟수
6+
7+ for (int i =0 ; i <nums .length ; i ++){
8+ maps .put (nums [i ], maps .getOrDefault (nums [i ], 0 ) + 1 );
9+ }
10+
11+
12+
13+ ArrayList <Integer > list = new ArrayList <>();
14+ //maps를 value기준으로 내림차순 정렬후, list에 k개만큼만 key값 추가
15+ Map <Integer , Integer > sorts =
16+ maps .entrySet ()
17+ .stream ()
18+ .sorted (Map .Entry .<Integer , Integer >comparingByValue ().reversed ())
19+ .limit (k )
20+ .collect (Collectors .toMap (
21+ Map .Entry ::getKey ,
22+ Map .Entry ::getValue ,
23+ (a , b ) -> a ,
24+ LinkedHashMap ::new
25+ ));
26+
27+ for (int t : sorts .keySet ()){
28+ list .add (t );
29+ }
30+
31+ return list .stream ()
32+ .mapToInt (Integer ::intValue )
33+ .toArray ();
34+
35+ //list.stream().mapToInt(Integer::intValue).toArray();
36+ //Arrays.stream(arr).boxed().toList();
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public boolean isAnagram (String s , String t ) {
5+ if (s .length () != t .length ()) return false ;
6+
7+ //s를 hashmap에 저장하고, t에서 하나씩 없애기
8+ HashMap <Character , Integer > maps = new HashMap <>();
9+
10+ for (int i =0 ; i < s .length (); i ++){
11+ char c = s .charAt (i );
12+
13+ if (maps .containsKey (c )) maps .put (c , maps .get (c )+1 );
14+ else maps .put (c , 1 );
15+
16+ }
17+
18+ for (int i = 0 ; i < t .length () ;i ++){
19+ char c = t .charAt (i );
20+ if (maps .containsKey (c )){
21+ if (maps .get (c ) > 1 ) maps .put (c , maps .get (c ) - 1 );
22+ else maps .remove (c );
23+ }
24+ }
25+
26+
27+ if (maps .size () == 0 ) return true ;
28+
29+ return false ;
30+
31+
32+
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments