Skip to content

Commit 979169f

Browse files
authored
Merge pull request #2354 from juhui-jeong/main
[juhui-jeong] WEEK 1 Solutions
2 parents b6d6593 + 4f0c677 commit 979169f

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

two-sum/juhui-jeong.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)