-
-
Notifications
You must be signed in to change notification settings - Fork 195
[clara-shin] WEEK11 Solutions #1563
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* n개의 서로 다른 숫자가 들어있는 배열에서, 0부터 n까지의 범위 중 빠진 숫자 하나를 찾는 문제 | ||
* 배열의 길이가 n이면, 실제로는 0부터 n까지 총 (n+1)개의 숫자 중 하나가 빠져있음 | ||
* | ||
* 접근방법: XOR 비트 연산 | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(1) | ||
* | ||
* XOR의 성질: | ||
* - a ^ a = 0 (같은 수끼리 XOR하면 0) | ||
* - a ^ 0 = a (어떤 수든 0과 XOR하면 자기 자신) | ||
* - XOR은 교환법칙과 결합법칙이 성립 | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var missingNumber = function (nums) { | ||
let result = nums.length; // n으로 초기화 | ||
|
||
// 인덱스 i와 nums[i]를 모두 XOR | ||
// 결국 빠진 숫자만 홀수 번 나타나서 결과로 남음 | ||
for (let i = 0; i < nums.length; i++) { | ||
result ^= i ^ nums[i]; | ||
} | ||
|
||
return result; | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 for loop을 두번에 나눠서 인덱스와 nums의 원소로 분리해서 xor 했는데 이렇게 한번에 합칠수가 있네요. 한 수 배워갑니다. 👍