Skip to content

[JustHm] Week 14 Solutions #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions binary-tree-level-order-traversal/JustHm.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 재귀 함수로 해결
// 각 노드의 레벨을 기억하고있다가 정답변수에 레벨별로 노드값을 넣어줌
class Solution {
func levelOrder(_ root: TreeNode?) -> [[Int]] {
var answer = [[Int]]()
searchNodes(0, root, &answer)
return answer
}

func searchNodes(_ level: Int, _ node: TreeNode?, _ answer: inout [[Int]]) {
guard let node else { return }
if level >= answer.count { answer.append([]) }
answer[level].append(node.val)
searchNodes(level + 1, node.left, &answer)
searchNodes(level + 1, node.right, &answer)
}
}
14 changes: 14 additions & 0 deletions counting-bits/JustHm.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// time: O(n)
// approach - 2자리 3자리 ... 로 2진법 변환시 맨 앞 1은 고정이다.
// 그 이후 나머지는 0~N자리일때 총 1의 갯수 를 더해줬다.
class Solution {
func countBits(_ n: Int) -> [Int] {
var answer: [Int] = [0, 1]

while answer.count < n + 1 {
answer += answer.map{$0 + 1}
}

return [Int](answer.prefix(n + 1))
}
}