Skip to content

Commit d72903d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents a340f0c + 36cfe67 commit d72903d

File tree

11 files changed

+250
-0
lines changed

11 files changed

+250
-0
lines changed

3sum/easyone-jwlee.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 풀이
2+
// 배열을 정렬하고
3+
// two pointer 사용
4+
5+
// TC
6+
// 정렬 O(nlogn) + Two pointer 이중 루프 O(n^2) = O(n^2)
7+
8+
// SC
9+
// Go의 sort.Ints()는 TimSort를 사용.
10+
// Merge Sort와 Insertion Sort의 조합으로 동작.
11+
// 정렬 O(n) + Two pointer O(1) + 결과 배열 O(n) = O(n)
12+
13+
func threeSum(nums []int) [][]int {
14+
result := [][]int{}
15+
sort.Ints(nums) // nums를 정렬
16+
17+
for i := 0; i < len(nums)-2; i++ {
18+
if i > 0 && nums[i] == nums[i-1] {
19+
continue // 중복된 값 건너뜀
20+
}
21+
22+
left, right := i+1, len(nums)-1
23+
for left < right {
24+
sum := nums[i] + nums[left] + nums[right]
25+
if sum == 0 {
26+
result = append(result, []int{nums[i], nums[left], nums[right]})
27+
left++
28+
right--
29+
30+
// 중복 제거
31+
for left < right && nums[left] == nums[left-1] {
32+
left++
33+
}
34+
for left < right && nums[right] == nums[right+1] {
35+
right--
36+
}
37+
} else if sum < 0 {
38+
left++
39+
} else {
40+
right--
41+
}
42+
}
43+
}
44+
45+
return result
46+
}

climbing-stairs/easyone-jwlee.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 풀이
2+
// 1 일 때, 가능한 step은 1 -> 1가지
3+
// 2 일 때, 가능한 step은 1 1, 2 -> 2가지
4+
// 3 일 때, 가능한 step은 1 1 1, 1 2, 2 1 -> 3가지
5+
// n 일 때, 가능한 stop은 n-1의 가짓수에 1이 붙고, n-2의 가짓수에 2가 붙는다.
6+
7+
// TC
8+
// O(n)
9+
10+
// SC
11+
// int타입 변수만 사용해서 O(1)
12+
13+
func climbStairs(n int) int {
14+
if n <= 2 {
15+
return n
16+
}
17+
prev := 1 // climb1
18+
curr := 2 // climb2
19+
for i := 3; i <= n; i++ {
20+
prev, curr = curr, (curr + prev)
21+
}
22+
return curr
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 풀이
2+
// preorder[0]이 제일 꼭대기
3+
// preorder 배열의 root index, inorder 배열의 시작, inorder 배열의 끝
4+
// 위의 세가지를 parameter로 받는 재귀함수를 만들어서
5+
// 왼쪽 서브트리, 오른쪽 서브트리를 순회
6+
7+
// TC
8+
// map 만들기 O(n) + 재귀함수는 각 노드를 딱 한번씩만 방문하므로 O(n) = O(n)
9+
10+
// SC
11+
// map O(n) + 재귀함수 최악의 경우 한쪽으로만 노드가 이어지는 경우 O(n) = O(n)
12+
13+
func buildTree(preorder []int, inorder []int) *TreeNode {
14+
inorderMap := make(map[int]int)
15+
for i, n := range inorder {
16+
inorderMap[n] = i
17+
}
18+
19+
var recursive func(rootIndex, inStart, inEnd int) *TreeNode
20+
recursive = func(rootIndex, inStart, inEnd int) *TreeNode {
21+
if rootIndex >= len(preorder) || inStart > inEnd {
22+
return nil
23+
}
24+
rootVal := preorder[rootIndex]
25+
rootInorderIndex := inorderMap[rootVal]
26+
27+
result := &TreeNode{
28+
Val: rootVal,
29+
}
30+
31+
leftSize := rootInorderIndex - inStart
32+
33+
result.Left = recursive(rootIndex+1, inStart, rootInorderIndex-1)
34+
result.Right = recursive(rootIndex+1+leftSize, rootInorderIndex+1, inEnd)
35+
36+
return result
37+
}
38+
39+
return recursive(0, 0, len(inorder)-1)
40+
}

contains-duplicate/EstherKim97.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
2+
3+
def containsDuplicate(nums):
4+
seen = set()
5+
for num in nums:
6+
if num in seen:
7+
return True
8+
seen.add(num)
9+
return False
10+

contains-duplicate/hancrysta1.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.*;
2+
class Solution {
3+
public boolean containsDuplicate(int[] nums) {
4+
Set<Integer> numSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
5+
if(numSet.size()!=nums.length) return true;
6+
else return false;
7+
}
8+
}

decode-ways/easyone-jwlee.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 풀이
2+
// dp로 풀이
3+
// 두자리 수에 적합하면 prev2(i-2)에 해당하는 값을 더하기
4+
5+
// TC
6+
// O(n)
7+
8+
// SC
9+
// data type이 int인 변수만 사용했으므로 O(1)
10+
11+
func numDecodings(s string) int {
12+
if len(s) == 0 || s[0] == '0' {
13+
return 0
14+
}
15+
prev2, prev1 := 1, 1
16+
for i := 1; i < len(s); i++ {
17+
curr := 0
18+
19+
// 한자리수 확인
20+
if s[i] != '0' {
21+
curr += prev1
22+
}
23+
24+
// 두자리수 확인
25+
digit, _ := strconv.Atoi(s[i-1 : i+1])
26+
if digit >= 10 && digit <= 26 {
27+
curr += prev2
28+
}
29+
30+
prev2, prev1 = prev1, curr
31+
}
32+
return prev1
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
unique_no = set(nums)
4+
data = []
5+
6+
for i in unique_no:
7+
count = 0
8+
for j in nums:
9+
if i == j:
10+
count += 1
11+
data.append((count, i))
12+
13+
data = sorted(data, reverse=True, key=lambda x:[1])
14+
15+
return [x[1] for x in data[:k]]
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.*;
2+
class Solution {
3+
public int[] topKFrequent(int[] nums, int k) {
4+
Map<Integer,Integer> count = new HashMap<>();
5+
for(int i=0;i<nums.length;i++){
6+
count.put(nums[i],count.getOrDefault(nums[i],0)+1);
7+
}
8+
List<Integer> sortedCount = new ArrayList<>(count.keySet());
9+
sortedCount.sort((a,b)->count.get(b)-count.get(a));//value 기준 키 정렬
10+
int[] answer = new int[k];
11+
for(int i=0;i<k;i++){
12+
answer[i] = sortedCount.get(i);
13+
}
14+
15+
return answer;
16+
}
17+
}

valid-anagram/easyone-jwlee.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 풀이
2+
// s의 rune을 key로 같은 철자의 갯수를 담게하고
3+
// t와 비교하면서 갯수를 하나씩 감소하게 해서
4+
// 남은 철자가 있거나 (s에는 있고, r에는 없는 철자가 있는 경우)
5+
// 더 감소된 철자가 있으면 (s에는 없고, r에만 있는 철자가 있는 경우) false가 return 되게 함.
6+
7+
// TC
8+
// O(n+n+n) = O(n)
9+
10+
// SC
11+
// s의 길이만큼 늘어나는 map으로 인해 O(n)
12+
13+
func isAnagram(s string, t string) bool {
14+
m := make(map[rune]int)
15+
for _, r := range s {
16+
m[r]++
17+
}
18+
for _, r := range t {
19+
m[r]--
20+
}
21+
for _, count := range m {
22+
if count < 0 || count > 0 {
23+
return false
24+
}
25+
}
26+
return true
27+
}

valid-palindrome/EstherKim97.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def isPalindrome(self, s):
3+
import re
4+
# remove all non-alphanumeric characters
5+
s = re.sub(r'\W+', '', s.lower())
6+
7+
# check if the string is equal forward and backward
8+
for i in range(len(s)//2):
9+
if s[i] != s[-(i+1)]:
10+
return False
11+
return True
12+

valid-palindrome/hancrysta1.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.*;
2+
class Solution {
3+
public boolean isPalindrome(String s) {
4+
String words = s.toLowerCase().replaceAll("[^0-9A-Za-z]","");
5+
//System.out.println(words);
6+
Deque<Character> stack = new ArrayDeque<>();
7+
for(int i=0;i<words.length();i++){
8+
stack.push(words.charAt(i));
9+
}
10+
while(!stack.isEmpty()){
11+
for(int i=0;i<words.length();i++){
12+
if(words.charAt(i)==stack.pop()) continue;
13+
else return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}

0 commit comments

Comments
 (0)