File tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public boolean containsDuplicate (int [] nums ) {
5+ /*
6+ * -- 풀이 --
7+ * nums를 순회하면서 HashSet에 데이터를 넣어서 중복되었는 지 확인한다.
8+ *
9+ * -- 시간 복잡도 --
10+ * 길이 N인 nums를 순환하는데 대한 시간 복잡도 => O(N)
11+ * hashSet의 add에 대한 시간 복잡도 => O(1)
12+ * 전체 시간 복잡도 O(1)*O(N) =O(n)
13+ * ------------------------------------------
14+ *
15+ * -- 공간 복잡도 --
16+ * 길이 N인 nums를 넣을 Hashset이 있어야 하기에 O(N)
17+ * -------------------------------------------
18+ */
19+
20+ // 중복을 확인할 수 있는 set 선언
21+ HashSet <Integer > hashSet = new HashSet <>();
22+
23+ for (int num : nums ) {
24+ // set에 있다면
25+ if (!hashSet .add (num )) return true ;
26+ }
27+
28+ return false ;
29+ }
30+ }
31+
Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+
3+ class Solution {
4+ private int [] dp ;
5+
6+ public int rob (int [] nums ) {
7+ // 점화식
8+ // f(x) = f(나를 선택) + f(나를 안선택)
9+ // 100개라서 가능은 할 거 같다.
10+
11+ dp = new int [100 ];
12+
13+ // 0도 가능 하다
14+ Arrays .fill (dp , -1 );
15+
16+
17+ return recurse (nums , 0 );
18+
19+ }
20+
21+ public int recurse (int [] nums , int index ) {
22+ // 종료 조건
23+ if (index >= nums .length ) return 0 ;
24+
25+ // 이미 한번 처리가 되었다면
26+ if (dp [index ] != -1 ) return dp [index ];
27+
28+ int result = 0 ;
29+
30+ // 나를 선택하는 경우,
31+ result = Math .max (recurse (nums , index +2 )+nums [index ], result );
32+
33+ // 나를 선택하지 않는 경우,
34+ result = Math .max (recurse (nums , index +1 ), result );
35+
36+ dp [index ] = result ;
37+ return result ;
38+ }
39+ }
40+
Original file line number Diff line number Diff line change 1+ import java .util .HashSet ;
2+
3+ public class Solution {
4+ public int longestConsecutive (int [] nums ) {
5+ HashSet <Integer > set = new HashSet <>();
6+ for (int num : nums ) {
7+ set .add (num );
8+ }
9+
10+ int maxStreak = 0 ;
11+
12+ for (int num : nums ) {
13+ // 내가 시작 값이라면
14+ if (!set .contains (num - 1 )) {
15+ int currentNum = num ;
16+ int currentStreak = 1 ;
17+
18+ // 나로부터 연결되는 값을 찾는다.
19+ while (set .contains (currentNum + 1 )) {
20+ currentNum ++;
21+ currentStreak ++;
22+ }
23+
24+ maxStreak = Math .max (maxStreak , currentStreak );
25+ }
26+ }
27+
28+ return maxStreak ;
29+ }
30+ }
31+
32+
33+
34+
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Collections ;
3+ import java .util .HashMap ;
4+ import java .util .List ;
5+ import java .util .Map ;
6+
7+ class Solution {
8+ public int [] topKFrequent (int [] nums , int k ) {
9+ HashMap <Integer , Integer > hm = new HashMap <>();
10+
11+ for (int num : nums ) {
12+ hm .put (num , hm .getOrDefault (num , 0 )+1 );
13+ }
14+
15+ List <Map .Entry <Integer , Integer >> list = new ArrayList <>(hm .entrySet ());
16+ list .sort (Map .Entry .comparingByValue (Collections .reverseOrder ()));
17+
18+ int index = 1 ;
19+ ArrayList <Integer > answer = new ArrayList <>();
20+ for (Map .Entry <Integer , Integer > entry : list ) {
21+ if (index > k ) break ;
22+ answer .add (entry .getKey ());
23+ index ++;
24+ }
25+
26+ return answer .stream ().mapToInt (Integer ::intValue ).toArray ();
27+ }
28+ }
29+
Original file line number Diff line number Diff line change 1+ /* 첫 시도
2+ leetCode 기준 18ms
3+ */
4+
5+ class Solution {
6+ public boolean isPalindrome (String s ) {
7+ // 1. remove non-alphanumeric using regex
8+ String alphanumeric = s .replaceAll ("[^a-zA-Z0-9]" , "" );
9+
10+ // 2. change lowerCase
11+ String lowerCase = alphanumeric .toLowerCase ();
12+
13+ // 3. compare reverse String
14+ return lowerCase .contentEquals (new StringBuffer (lowerCase ).reverse ());
15+ }
16+ }
17+
You can’t perform that action at this time.
0 commit comments