Skip to content

Commit 810602d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 3e7983c + 3c65a62 commit 810602d

File tree

12 files changed

+324
-0
lines changed

12 files changed

+324
-0
lines changed

3sum/WhiteHyun.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// 15. 3Sum
3+
// https://leetcode.com/problems/3sum/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/04.
7+
//
8+
9+
class Solution {
10+
func threeSum(_ nums: [Int]) -> [[Int]] {
11+
var result: [[Int]] = []
12+
let sorted = nums.sorted()
13+
14+
for (index, element) in sorted.enumerated() where index <= 0 || element != sorted[index - 1] {
15+
var left = index + 1
16+
var right = sorted.count - 1
17+
18+
while left < right {
19+
let threeSum = element + sorted[left] + sorted[right]
20+
if threeSum > 0 {
21+
right -= 1
22+
continue
23+
}
24+
if threeSum < 0 {
25+
left += 1
26+
continue
27+
}
28+
29+
result.append([element, sorted[left], sorted[right]])
30+
left += 1
31+
32+
while sorted[left] == sorted[left - 1] && left < right {
33+
left += 1
34+
}
35+
}
36+
}
37+
38+
39+
return result
40+
}
41+
}

3sum/han.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Solution do
2+
@spec three_sum(nums :: [integer]) :: [[integer]]
3+
def three_sum(nums) do
4+
length_of_nums = length nums
5+
num_index_map = nums |> Enum.with_index |> Map.new
6+
frequencies_of_num = nums |> Enum.frequencies()
7+
tuple_nums = nums |> List.to_tuple
8+
num_indexs_map = nums |>
9+
Enum.with_index() |>
10+
Enum.group_by(fn {v, _} -> v end, fn {_, i} -> i end)
11+
12+
Stream.unfold({0, 1}, fn {i, j} ->
13+
if j < length_of_nums - 1,
14+
do: {{i, j}, {i, j + 1}},
15+
else: {{i, j}, {i + 1, i + 2}}
16+
end) |>
17+
Stream.take_while(fn {i, _} ->
18+
i < length_of_nums - 1
19+
end) |>
20+
Stream.map(fn {i, j} ->
21+
a = elem tuple_nums, i
22+
b = elem tuple_nums, j
23+
c = -(a + b)
24+
25+
case frequencies_of_num[c] do
26+
nil -> nil
27+
count when count >= 3 -> [a, b, c] |> Enum.sort()
28+
_ ->
29+
if num_indexs_map[c] |> Enum.filter(& &1 != i && &1 != j) |> Enum.at(0),
30+
do: [a, b, c] |> Enum.sort(),
31+
else: nil
32+
end
33+
end) |>
34+
Stream.reject(& &1 == nil) |>
35+
Stream.uniq |>
36+
Enum.to_list
37+
end
38+
end

counting-bits/bky373.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/
3+
*
4+
* time: O(n * log n)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int[] countBits(int n) {
10+
int[] ans = new int[n + 1];
11+
for (int i = 0; i <= n; i++) {
12+
int x = i;
13+
int cnt = 0;
14+
while (x != 0) {
15+
cnt++;
16+
x &= (x - 1);
17+
}
18+
ans[i] = cnt;
19+
}
20+
return ans;
21+
}
22+
}
23+

encode-and-decode-strings/han.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
class Solution:
3+
"""
4+
@param: strs: a list of strings
5+
@return: encodes a list of strings to a single string.
6+
"""
7+
def encode(self, strs):
8+
return '-'.join([
9+
'.'.join([
10+
str(ord(c))
11+
for c in s
12+
])
13+
for s in strs
14+
])
15+
16+
"""
17+
@param: str: A string
18+
@return: decodes a single string to a list of strings
19+
"""
20+
def decode(self, str):
21+
return [
22+
''.join([
23+
chr(int(c))
24+
for c in s.split('.')
25+
])
26+
for s in str.split('-')
27+
]
28+
29+
30+
# 아래는 간단한 테스트 코드
31+
import random
32+
33+
def generate_random_string(min_length=5, max_length=20):
34+
# Choose a random string length between min_length and max_length
35+
string_length = random.randint(min_length, max_length)
36+
37+
# Define the character categories
38+
alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
39+
numbers = '0123456789'
40+
hanguls = '가나다라마바사아자차카타파하'
41+
emojis = ['😀', '😂', '😉', '😍', '😎', '😭', '😊', '😜', '😢', '😱']
42+
43+
# Probability of selecting from each category
44+
weights = [0.25, 0.25, 0.25, 0.25]
45+
46+
# Combine all categories into a single list
47+
all_chars = list(alphabets + numbers + hanguls) + emojis
48+
49+
# Select characters based on the weights and create the final string
50+
random_chars = random.choices(all_chars, k=string_length)
51+
random_string = ''.join(random_chars)
52+
53+
return random_string
54+
55+
def generate_multiple_random_strings():
56+
strings_list = []
57+
# Generate a random number of strings, between 1 and 10
58+
number_of_strings = random.randint(1, 10)
59+
60+
for _ in range(number_of_strings):
61+
random_string = generate_random_string()
62+
strings_list.append(random_string)
63+
64+
return strings_list
65+
66+
strs = generate_multiple_random_strings()
67+
print(strs)
68+
print(strs==Solution().decode(Solution().encode(strs)))

group-anagrams/bky373.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* https://leetcode.com/problems/group-anagrams/
3+
*
4+
* time: O(n * m log m)
5+
* space: O(nm)
6+
*/
7+
class Solution {
8+
9+
public List<List<String>> groupAnagrams(String[] strs) {
10+
Map<String, List<String>> groups = new HashMap();
11+
for (String str : strs) {
12+
char[] arr = str.toCharArray();
13+
Arrays.sort(arr);
14+
groups.computeIfAbsent(new String(arr), k -> new ArrayList<>())
15+
.add(str);
16+
}
17+
return new ArrayList(groups.values());
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// 128. Longest Consecutive Sequence
3+
// https://leetcode.com/problems/longest-consecutive-sequence/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/01.
7+
//
8+
9+
final class Solution {
10+
func longestConsecutive(_ nums: [Int]) -> Int {
11+
let set = Set(nums)
12+
var best = 0
13+
for number in set where !set.contains(number - 1) {
14+
var next = number + 1
15+
while set.contains(next) {
16+
next += 1
17+
}
18+
if best < next - number {
19+
best = next - number
20+
}
21+
}
22+
23+
return best
24+
}
25+
}

missing-number/bky373.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* https://leetcode.com/problems/missing-number/
3+
*
4+
* time: O(n)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
public int missingNumber(int[] nums) {
9+
int sum = nums.length * (nums.length+1) / 2;
10+
for (int num : nums) {
11+
sum -= num;
12+
}
13+
return sum;
14+
}
15+
}

number-of-1-bits/bky373.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/number-of-1-bits/
3+
*
4+
* time: O(log n)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int hammingWeight(int n) {
10+
int cnt = 0;
11+
while (n != 0) {
12+
cnt++;
13+
n &= (n - 1);
14+
}
15+
return cnt;
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// 238. Product of Array Except Self
3+
// https://leetcode.com/problems/product-of-array-except-self/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/01.
7+
//
8+
9+
final class Solution {
10+
func productExceptSelf(_ nums: [Int]) -> [Int] {
11+
var answer: [Int] = .init(repeating: 1, count: nums.count)
12+
13+
var left_product = 1
14+
for i in nums.indices {
15+
answer[i] = left_product
16+
left_product *= nums[i]
17+
}
18+
19+
var right_product = 1
20+
for i in nums.indices.reversed() {
21+
answer[i] *= right_product
22+
right_product *= nums[i]
23+
}
24+
25+
return answer
26+
}
27+
}

reverse-bits/bky373.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-bits/
3+
*
4+
* time: O(1)
5+
* space: O(1)
6+
*/
7+
public class Solution {
8+
9+
public int reverseBits(int n) {
10+
int ans = 0;
11+
for (int i = 0; i < 32; i++) {
12+
ans = ans << 1 | n & 1;
13+
n >>= 1;
14+
}
15+
return ans;
16+
}
17+
}

0 commit comments

Comments
 (0)