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+ /*
2+
3+ // 첫번째 풀이
4+ class Solution {
5+ public boolean containsDuplicate(int[] nums) {
6+ for (int i=0; i < nums.length; i++) {
7+ for (int j=i+1; j < nums.length; j++) {
8+ if (nums[i] == nums[j]) return true;
9+ }
10+ }
11+ return false;
12+ }
13+ }
14+
15+ // 두번째 풀이
16+ class Solution {
17+ public boolean containsDuplicate(int[] nums) {
18+ Set set = new HashSet<Integer>();
19+ for (int i=0; i < nums.length; i++) {
20+ set.add(nums[i]);
21+ }
22+
23+ if (set.size() != nums.length) return true;
24+ return false;
25+ }
26+ }
27+
28+ */
29+
30+ class Solution {
31+ public boolean containsDuplicate (int [] nums ) {
32+ Set <Integer > set = new HashSet <>();
33+ for (int i =0 ; i < nums .length ; i ++) {
34+ if (!set .add (nums [i ])) return true ;
35+ }
36+ return false ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int longestConsecutive (int [] nums ) {
3+ if (nums == null || nums .length == 0 ) return 0 ;
4+ Arrays .sort (nums );
5+
6+ int maxLen = 1 ;
7+ int curLen = 1 ;
8+
9+ for (int i = 1 ; i < nums .length ; i ++) {
10+ if (nums [i ] == nums [i -1 ]) {
11+ // 중복 건너뛰기
12+ continue ;
13+ }
14+
15+ if (nums [i ] == nums [i -1 ] +1 ) {
16+ curLen ++;
17+ } else {
18+ maxLen = Math .max (maxLen , curLen );
19+ curLen = 1 ;
20+ }
21+ }
22+
23+ return Math .max (maxLen , curLen );
24+ }
25+ }
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 i = 0 ; i < nums .length ; i ++) {
6+ if (map .containsKey (nums [i ])) {
7+ map .put (nums [i ], map .get (nums [i ])+1 );
8+ } else {
9+ map .put (nums [i ], 1 );
10+ }
11+ }
12+
13+ List <Map .Entry <Integer , Integer >> entryList = new LinkedList <>(map .entrySet ());
14+ entryList .sort (Map .Entry .comparingByValue (Collections .reverseOrder ()));
15+
16+ int [] result = new int [k ];
17+
18+ for (int i =0 ; i < k ; i ++) {
19+ result [i ] = entryList .get (i ).getKey ();
20+ }
21+
22+ return result ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] twoSum (int [] nums , int target ) {
3+ for (int i = 0 ; i < nums .length ; i ++) {
4+ for (int j =i +1 ; j < nums .length ; j ++) {
5+ if (nums [i ] + nums [j ] == target ) {
6+ return new int [] {i , j };
7+ }
8+ }
9+ }
10+ return new int [0 ];
11+ }
12+ }
You can’t perform that action at this time.
0 commit comments