File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
Expand file tree Collapse file tree 3 files changed +50
-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 Solution {
5+ public boolean containsDuplicate (int [] nums ) {
6+ Set set = new HashSet ();
7+ for (int num : nums ) {
8+ set .add (num );
9+ }
10+
11+ if (set .size () != nums .length ) {
12+ return true ;
13+ } else {
14+ return false ;
15+ }
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] topKFrequent (int [] nums , int k ) {
3+ Map <Integer , Integer > map = new HashMap <>();
4+
5+ for (int num : nums ) {
6+ int current = map .getOrDefault (num , 0 );
7+ map .put (num , current + 1 );
8+ }
9+
10+ List <Integer > keySet = new ArrayList <>(map .keySet ());
11+ keySet .sort ((o1 , o2 ) -> map .get (o2 ).compareTo (map .get (o1 )));
12+
13+ return keySet .subList (0 , k )
14+ .stream ()
15+ .mapToInt (Integer ::intValue )
16+ .toArray ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] twoSum (int [] nums , int target ) {
3+ int [] answer = new int [2 ];
4+ for (int i = 0 ; i < nums .length ; i ++) {
5+ for (int j = i + 1 ; j < nums .length ; j ++) {
6+ if (nums [i ] + nums [j ] == target ) {
7+ answer [0 ] = i ;
8+ answer [1 ] = j ;
9+ break ;
10+ }
11+ }
12+ }
13+ return answer ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments