File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Expand file tree Collapse file tree 3 files changed +82
-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 SolutionGotprgmer {
5+ // ํด๋น ๋ฌธ์ ๋ ์ด๋ ํ ์ซ์๊ฐ 2๊ฐ์ด์ ์กด์ฌํ ๊ฒฝ์ฐ true๋ฅผ ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, false๋ฅผ ๋ฐํํ๋ ๋ฌธ์ ์ด๋ค.
6+ // set์ ์ฌ์ฉํด์ set์ ์ด๋ฏธ ๊ฐ์ด ์กด์ฌํ๋ค๋ฉด ๊ฐ์๊ฐ 2 ์ด์์ด๋ฏ๋ก true ๊ทธ๋ ์ง ์์ผ๋ฉด false๋ฅผ ์ถ๋ ฅํ๋ค.
7+
8+ // ๊ฐ ์ซ์๋ค์ ์ ์ฅํด์ set์ผ๋ก ๊ด๋ฆฌ -> distinctNums
9+ // nums์ ๊ฐ ์ซ์์ธ checkNum์ distinctNums์ ๋ฃ์ด์ค๋ค.
10+ // ๋ง์ฝ checkNum์ด ์ด๋ฏธ distinctNums์ ์กด์ฌํ๋ค๋ฉด ans๋ฅผ true๋ก ๋ง๋ค์ด์ฃผ๊ณ ๋ต์ ์ถ๋ ฅํ๋ค.
11+
12+
13+ // ์๊ฐ๋ณต์ก๋ -> O(n)
14+ // ๊ณต๊ฐ๋ณต์ก๋ -> O(n)
15+ static Set <Integer > distinctNums ;
16+ public boolean containsDuplicate (int [] nums ) {
17+ distinctNums = new HashSet <>();
18+ boolean ans = false ;
19+ for (int checkNum : nums ) {
20+ if (distinctNums .contains (checkNum )) {
21+ ans = true ;
22+ break ;
23+ };
24+ distinctNums .add (checkNum );
25+ }
26+ return ans ;
27+ }
28+
29+
30+ }
Original file line number Diff line number Diff line change 1+ // ๊ฐ ์์ ๊ฐ์๋ฅผ ์นด์ดํธ ํ๊ณ ์นด์ดํธ ํ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ์ฐ์ ์์ ํ๋ก
2+ // k๊ฐ๋ฅผ ์ถ์ถํ์ฌ result ๋ฆฌ์คํธ์ ๋ด๋๋ค.
3+
4+ // ์๊ฐ๋ณต์ก๋ : O(NlogN)
5+ // ๊ณต๊ฐ๋ณต์ก๋ : O(N)
6+
7+
8+ public class SolutionGotprgmer {
9+
10+ public int [] topKFrequent (int [] nums , int k ) {
11+ Map <Integer , Integer > map = new HashMap <>();
12+ for (int num :nums ){
13+ map .put (num ,map .getOrDefault (num ,0 )+1 );
14+ }
15+ PriorityQueue <Map .Entry <Integer , Integer >> pq = new PriorityQueue <>(
16+ (e1 , e2 ) -> e2 .getValue ().compareTo (e1 .getValue ())
17+ );
18+ for (Map .Entry <Integer ,Integer > entry : map .entrySet ()){
19+ pq .offer (entry );
20+ }
21+
22+ int [] result = new int [k ];
23+ for (int ansIdx =0 ;ansIdx < k ; ansIdx ++){
24+ result [ansIdx ] = pq .poll ().getKey ();
25+ }
26+ return result ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ // ์
๋ ฅ๋ ๋ฌธ์ ์ค ์ํ๋ฒณ์ด๋ ์ซ์๊ฐ ์๋ ๊ฐ๋ค์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
2+ // ์ด๋ ๋ชจ๋ ๋ฌธ์๋ค์ ์๋ฌธ์๋ก ๋ฐ๊พผ๋ค.
3+ // ์ญ์ ๋ ฌํ ๊ฐ๊ณผ ์๋ ๊ฐ์ ๋น๊ตํด ํฐ๋ฆฐ๋๋กฌ ์ ๋ฌด๋ฅผ ์ถ๋ ฅํ๋ค.
4+
5+ // ์ํ๋ฒณ ํน์ ์ซ์๊ฐ ์๋์ง ๊ฒ์ฌํ๋ ๋ฌธ์ -> charToCheck
6+
7+ // ์๊ฐ๋ณต์ก๋ : O(n)
8+ // ๊ณต๊ฐ๋ณต์ก๋ : O(n)
9+
10+ class Solution_Gotprgmer {
11+ public boolean validPalindrome (String s ) {
12+ StringBuilder sb = new StringBuilder ();
13+ for (char charToCheck : s .toCharArray ()){
14+ if (!Character .isLetterOrDigit (charToCheck )){
15+ continue ;
16+ }
17+ sb .append (Character .toLowerCase (charToCheck ));
18+ }
19+ String originalDirection = sb .toString ();
20+ String reDirection = sb .reverse ().toString ();
21+
22+ return originalDirection .equals (reDirection );
23+ }
24+ }
You canโt perform that action at this time.
0 commit comments