-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from jaejeong1/main
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class SolutionJaeJeong1 { | ||
public boolean containsDuplicate(int[] nums) { | ||
// ํด์๋งต ์ฌ์ฉํด์ ๊ฐ์ ๊ฐ์ ์นด์ดํธ๊ฐ 1๋ณด๋ค ํฌ๋ฉด true ๋ฐํ | ||
// ๋๊น์ง ๋ค ๋๋ฉด false ๋ฐํ | ||
// ๋๋ ํด์์ ์ฌ์ฉํด์ ๋ชจ๋ ํด์์ ์ ๋ฃ๊ณ | ||
// ๊ธธ์ด ๋น๊ตํด์ ๊ฐ์ผ๋ฉด false, ๋ค๋ฅด๋ฉด true ๋ฐํ | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N) | ||
|
||
Set<Integer> set = Arrays.stream(nums).collect(HashSet::new, Set::add, Set::addAll); | ||
return set.size() != nums.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// Definition for a binary tree node. | ||
class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
TreeNode() {} | ||
TreeNode(int val) { this.val = val; } | ||
TreeNode(int val, TreeNode left, TreeNode right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
class SolutionKthSmallest { | ||
public int kthSmallest(TreeNode root, int k) { | ||
// ์ด์ง ๊ฒ์ ํธ๋ฆฌ์ ๋ฃจํธ์ ์ ์ k๊ฐ ์ฃผ์ด์ง๋ฉด ํธ๋ฆฌ์ ์๋ ๋ชจ๋ ๋ ธ๋ ๊ฐ ์ค k๋ฒ์งธ๋ก ์์ ๊ฐ(1-์ธ๋ฑ์ค)์ ๋ฐํํฉ๋๋ค. | ||
// ์ด์ง ๊ฒ์ ํธ๋ฆฌ์ ํน์ฑ์ ์ด์ฉํด ์ค์ ์ํ๋ฅผ ํ๋ฉด ๋ ธ๋ ๊ฐ์ด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋๋ค. | ||
// ์ ๋ ฌ ํ k๋ฒ์งธ ๊ฐ์ ๋ฐํํ๋ค. | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N) | ||
List<Integer> list = new ArrayList<>(); | ||
inorder(root, list); | ||
return list.get(k - 1); | ||
} | ||
|
||
private void inorder(TreeNode root, List<Integer> list) { | ||
if (root == null) { | ||
return; | ||
} | ||
inorder(root.left, list); | ||
list.add(root.val); | ||
inorder(root.right, list); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class SolutionJaejeong1 { | ||
public int hammingWeight(int n) { | ||
// ์ฃผ์ด์ง ์์ ์ ์๋ฅผ ์ด์ง์๋ก ๋ณํํ๊ณ , 1๋ก ์ค์ ๋ ์๋ฆฟ์์ ๊ฐ์๋ฅผ ๋ฐํ | ||
// ์๊ฐ๋ณต์ก๋: O(1), ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
int num = 0; | ||
for (int i=0; i<32; i++) { | ||
if((n & 1) == 1) { | ||
num++; | ||
} | ||
n = n >> 1; | ||
} | ||
return num; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class SolutionPalindromicSubstrings { | ||
// 1๋ฒ์จฐ ํ์ด: ๊ตฌํํด์ผํ ๋ก์ง์ ํฐ๋ฆฐ๋๋กฌ ์ฌ๋ถ ๊ฒ์ฌ์ ๊ฒ์ฌํ ๋์ ๋ฌธ์์ด์ ๊ตฌํ๋ ๋ก์ง ๋๋ก ๋๋๋ค | ||
// ํฐ๋ฆฐ๋๋กฌ ์ฌ๋ถ ๊ฒ์ฌ: ํฌํฌ์ธํฐ ์ฌ์ฉ, lt=0, rt=length-1 ๋ก ์์ํด ๋๋ฑ์ฑ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌ | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
// ๋์ ๋ฌธ์์ด ๊ตฌํ๊ธฐ: ํฌํฌ์ธํฐ ์ฌ์ฉ. rt๊ฐ length๋ณด๋ค ๊ฐ๊ฑฐ๋ ์์๋๊น์ง ๊ณ์ ์ฆ๊ฐ์ํค๊ณ , | ||
// rt๊ฐ ๋์ ๋๋ฌํ๋ฉด lt๋ฅผ ์ฆ๊ฐ์ํค๊ณ , rt๋ฅผ lt+1๋ก ๋ง๋ ๋ค. ๋ชจ๋ ๊ณผ์ ์์ ํฐ๋ฆฐ๋๋กฌ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ค | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
// ๊ฒฐ๊ณผ | ||
// ์๊ฐ๋ณต์ก๋: O(N^2), ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
|
||
public int countSubstrings(String s) { | ||
var subStrings = s.toCharArray(); | ||
|
||
if (subStrings.length == 0) return 0; | ||
if (subStrings.length == 1) return 1; | ||
|
||
var answer = 0; | ||
|
||
var lt = 0; | ||
var rt = 1; | ||
while(lt < subStrings.length){ | ||
if (isPalindrome(s.substring(lt, rt))) { | ||
answer++; | ||
} | ||
|
||
if (rt <= subStrings.length-1){ | ||
rt++; | ||
} else { | ||
lt++; | ||
rt = lt+1; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
private boolean isPalindrome(String s) { | ||
var chars = s.toCharArray(); | ||
var lt = 0; | ||
var rt = chars.length - 1; | ||
|
||
while(lt < rt) { | ||
if (chars[lt] != chars[rt]) { | ||
return false; | ||
} | ||
lt++; | ||
rt--; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class SolutionTopKFrequentElements { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
// ๋น๋์์ผ๋ก k๊ฐ ๋ฐํ | ||
// ๋น๋ ์ฒดํฌ: ํด์๋งต์ผ๋ก ์นด์ดํธ. ์๊ฐ๋ณต์ก๋ O(N), ๊ณต๊ฐ๋ณต์ก๋ O(N) | ||
// ๋น๋์ ์ ๋ ฌ: sorting, ์๊ฐ๋ณต์ก๋ O(N log N), ๊ณต๊ฐ๋ณต์ก๋ O(N) | ||
// ํฉ์ฐ: ์๊ฐ๋ณต์ก๋ O(N log N), ๊ณต๊ฐ๋ณต์ก๋ O(N) | ||
|
||
// ๋น๋ ์ฒดํฌ | ||
Map<Integer, Integer> freq = new HashMap<>(); | ||
for (int num : nums) { | ||
freq.put(num, freq.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
// ๋น๋์ ์ ๋ ฌ | ||
return freq.keySet().stream() | ||
.sorted((a, b) -> freq.get(b) - freq.get(a)) | ||
.mapToInt(i -> i) | ||
.limit(k) // ๋ฐฐ์ด์์ ์์ k๊ฐ๋ง ๋ฐํ | ||
.toArray(); | ||
} | ||
} |