[WEEK07-2] 추슬기#32
Merged
Merged
Conversation
Member
Author
|
슬랙으로 말씀드렸었는데 PR이 밀리다보니 불편하시겠지만 코드 블록 내려보시면 해당 코드 보실 수 있습니다 ㅠ |
sik9252
approved these changes
Mar 30, 2026
Collaborator
There was a problem hiding this comment.
저랑 풀이가 같은 것 같습니다! 둘 다 null인지, 하나만 null인지, 값이 같은지를 먼저 확인한 뒤 왼쪽/오른쪽 서브트리를 비교하는 흐름이 명확해서 읽기 좋았어요~
Collaborator
There was a problem hiding this comment.
n & (n - 1) 비트 트릭을 활용해서 효율적으로 잘 구현하신 것 같아요, 근데 n >>> 0 은 어떤 의미인지 궁금합니다!
raejun92
approved these changes
Mar 30, 2026
| * @return {number} | ||
| */ | ||
| var hammingWeight = function(n) { | ||
| let current = n >>> 0; |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
이렇게 풀었어요
1. Same Tree
1) 복잡도 계산
2) 접근 아이디어
null이면 같은 노드로 보고true를 반환했다.null이거나 값이 다르면 바로false를 반환하고, 아니면 왼쪽/오른쪽 서브트리를 계속 비교했다.3) 회고
이번 문제는 혼자서는 결국 못 풀었다.
처음에는 왼쪽끼리 비교하고 오른쪽끼리 비교한다는 감각은 있었는데,
null처리와 현재 노드 값 비교를 어떤 순서로 묶어야 할지가 잘 안 잡혀서 흐름이 자꾸 끊겼다.풀이를 보고 나서는 이렇게 다시 정리할 수 있었다.
null이면 같은 트리라고 보고truenull이면 바로falsefalse트리 문제는 결국 “현재 노드에서 무엇을 비교할지”만 명확하게 잡히면
재귀 패턴으로 훨씬 깔끔하게 풀린다는 걸 다시 느꼈다.
2. Number of 1 Bits
1) 복잡도 계산
k는 켜진 비트 개수2) 접근 아이디어
n을 unsigned 정수로 바꿔서 비트 연산이 꼬이지 않게 맞췄다.current &= current - 1연산으로 가장 오른쪽의 1비트를 하나씩 지웠다.3) 회고
비트 문제는 아직 익숙하지 않아서 처음엔 문자열로 바꿔서 세는 방식이 먼저 떠오른다.
근데 이번에는
current & (current - 1)가 1비트를 하나씩 지운다는 패턴을 알고 나니생각보다 훨씬 짧고 명확하게 풀 수 있었다.
이런 비트 연산 문제는 공식처럼 외우기보다, 왜 오른쪽 1비트가 사라지는지 직접 다시 그려보면서 익숙해져야 할 것 같다.