File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .List ;
3+ import java .util .stream .Collectors ;
4+
5+ class Solution {
6+ public boolean containsDuplicate (int [] nums ) {
7+ List <Integer > list = Arrays .stream (nums ).boxed ().collect (Collectors .toList ());
8+ for (int i : nums ) {
9+ int idx = list .indexOf (i );
10+ if (idx == -1 ) {
11+ continue ;
12+ }
13+ list .remove (idx );
14+ if (list .contains (i )) {
15+ return true ;
16+ }
17+ }
18+ return false ;
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .util .stream .Collectors ;
3+
4+ class Solution {
5+ public int [] topKFrequent (int [] nums , int k ) {
6+ Set <Integer > unique = Arrays .stream (nums ).boxed ().collect (Collectors .toSet ());
7+ Map <Integer , Integer > value = new HashMap <>();
8+ for (int n : unique ) {
9+ int cnt = 0 ;
10+ for (int m : nums ) {
11+ if (n == m ) {
12+ cnt ++;
13+ }
14+ }
15+ value .put (n , cnt );
16+ }
17+ List <Map .Entry <Integer , Integer >> sortedByValueDesc = value .entrySet ()
18+ .stream ()
19+ .sorted (Map .Entry .comparingByValue (Comparator .reverseOrder ()))
20+ .toList ();
21+ return sortedByValueDesc .stream ()
22+ .limit (k )
23+ .map (Map .Entry ::getKey )
24+ .mapToInt (Integer ::intValue )
25+ .toArray ();
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ class Solution {
5+ public int [] twoSum (int [] nums , int target ) {
6+ // 시간 복잡도를 생각해서 HashMap을 사용
7+
8+ Map <Integer , Integer > idx = new HashMap <>();
9+ for (int i = 0 ; i < nums .length ; i ++) {
10+ int c = target - nums [i ];
11+ if (idx .containsKey (c )) {
12+ return new int []{idx .get (c ), i };
13+ }
14+ idx .put (nums [i ], i );
15+ }
16+ return new int []{};
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments