Skip to content

Commit 82d87ff

Browse files
authoredDec 21, 2024
Merge pull request #746 from bus710/week02
[bus710] Week 02
·
v4.13.0v3.2.0
2 parents f8a2b19 + 0b322c7 commit 82d87ff

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
 
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package hello
2+
3+
import (
4+
"errors"
5+
"log"
6+
)
7+
8+
type TreeNode struct {
9+
Val int
10+
Left *TreeNode
11+
Right *TreeNode
12+
}
13+
14+
func buildTree(preorder []int, inorder []int) *TreeNode {
15+
if len(preorder) == 1 {
16+
return &TreeNode{
17+
Val: preorder[0],
18+
}
19+
}
20+
21+
center, _ := findCenterWithValue(preorder[0], inorder)
22+
23+
head := TreeNode{
24+
Val: inorder[center],
25+
}
26+
27+
execute(&head, center, inorder)
28+
29+
return &head
30+
}
31+
32+
func findCenterWithValue(key int, inorder []int) (int, error) {
33+
for i, n := range inorder {
34+
if n == key {
35+
return i, nil
36+
}
37+
}
38+
return 0, errors.New("not found")
39+
}
40+
41+
func findCenter(slice []int) int {
42+
c := len(slice) / 2
43+
return c
44+
}
45+
46+
func getSplit(center int, inorder []int, currentVal int) ([]int, []int) {
47+
if len(inorder) == 1 {
48+
if inorder[0] == currentVal {
49+
return nil, nil
50+
}
51+
return []int{inorder[0]}, nil
52+
}
53+
if len(inorder) == 2 {
54+
if inorder[0] == currentVal {
55+
return nil, []int{inorder[1]}
56+
}
57+
if inorder[1] == currentVal {
58+
return []int{inorder[0]}, nil
59+
}
60+
return []int{inorder[0]}, []int{inorder[1]}
61+
}
62+
return inorder[:center], inorder[center+1:]
63+
}
64+
65+
func execute(currentNode *TreeNode, center int, slice []int) {
66+
left, right := getSplit(center, slice, currentNode.Val)
67+
68+
if len(left) == 1 {
69+
currentNode.Left = &TreeNode{Val: left[0]}
70+
} else if len(left) == 2 {
71+
currentNode.Left = &TreeNode{Val: left[0]}
72+
currentNode.Left.Right = &TreeNode{Val: left[1]}
73+
} else if len(left) > 2 {
74+
lc := findCenter(left)
75+
currentNode.Left = &TreeNode{Val: left[lc]}
76+
77+
if len(left) > 2 {
78+
execute(currentNode.Left, lc, left)
79+
}
80+
}
81+
82+
if len(right) == 1 {
83+
currentNode.Right = &TreeNode{Val: right[0]}
84+
} else if len(right) == 2 {
85+
currentNode.Right = &TreeNode{Val: right[0]}
86+
currentNode.Right.Left = &TreeNode{Val: right[1]}
87+
} else if len(right) > 2 {
88+
rc := findCenter(right)
89+
currentNode.Right = &TreeNode{Val: right[rc]}
90+
91+
if len(right) > 2 {
92+
execute(currentNode.Right, rc, right)
93+
}
94+
}
95+
}
96+
97+
func print(currentNode *TreeNode) {
98+
log.Println(currentNode.Val)
99+
if currentNode.Left != nil {
100+
print(currentNode.Left)
101+
}
102+
if currentNode.Right != nil {
103+
print(currentNode.Right)
104+
}
105+
}

‎counting-bits/bus710.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// T: O(n log n)
2+
// S: O(n)
3+
4+
pub fn count_bits(n: i32) -> Vec<i32> {
5+
// Prepare a vector.
6+
// The first item can just be 0
7+
let mut vec = Vec::from([0]);
8+
9+
// Iterate as many as the given number + 1
10+
for num in 1..n + 1 {
11+
// Get a binary string from the number (ex: 2 => 10)
12+
let num_str = format!("{num:b}");
13+
// Count '1' from the given binary string
14+
let cnt = num_str.chars().filter(|c| *c == '1').count();
15+
// Store the number in the vector
16+
vec.push(cnt as i32);
17+
}
18+
19+
vec
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn it_works() {
28+
let result = count_bits(2);
29+
assert_eq!(result, Vec::from([0, 1, 1]));
30+
31+
let result2 = count_bits(5);
32+
assert_eq!(result2, Vec::from([0, 1, 1, 2, 1, 2]));
33+
}
34+
}

‎valid-anagram/bus710.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Space complexity: O(2n) - 주어진 한 단어안의 각 문자가 서로 다 다를 경우 생성한 맵들의 최대 길이는 주어진 단어만큼이므로 2n
2+
// Time complexity: O(3n) - 각 맵을 생성하고 추가로 각 아이템을 비교하는 루프가 필요하므로 3n
3+
4+
use std::collections::HashMap;
5+
6+
pub fn is_anagram(s: String, t: String) -> bool {
7+
// Check if the lengh of the 2 words are same.
8+
// Otherwise return false.
9+
let len01 = s.len();
10+
let len02 = t.len();
11+
if len01 != len02 {
12+
return false;
13+
}
14+
15+
// Prepare and fill a new map for s
16+
let mut s_map = HashMap::new();
17+
for s_char in s.chars() {
18+
let n = s_map.get(&s_char).copied().unwrap_or(0);
19+
s_map.insert(s_char, n + 1);
20+
}
21+
22+
// Prepare and fill a new map for t
23+
let mut t_map = HashMap::new();
24+
for t_char in t.chars() {
25+
let n = t_map.get(&t_char).copied().unwrap_or(0);
26+
t_map.insert(t_char, n + 1);
27+
}
28+
29+
// Iterate over the map s, so compare with the map t
30+
// to see if both have same number for the same character respectively
31+
for (s_char, num) in s_map.iter() {
32+
if t_map.get(s_char).copied().unwrap_or(0) != *num {
33+
return false;
34+
}
35+
}
36+
37+
true
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_good() {
46+
let result = is_anagram("ana".to_owned(), "aan".to_owned());
47+
assert!(result);
48+
}
49+
50+
#[test]
51+
fn test_bad() {
52+
let result = is_anagram("aaa".to_owned(), "aan".to_owned());
53+
assert!(!result);
54+
}
55+
}
56+

0 commit comments

Comments
 (0)
Please sign in to comment.