-
-
Notifications
You must be signed in to change notification settings - Fork 195
[GUMUNYEONG] Week3 문제풀이 #398
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,49 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
let result = []; | ||
let numPair = {}; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
numPair[nums[i]] = target - nums[i]; | ||
} | ||
|
||
|
||
|
||
for (const key in numPair) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 비슷하게 풀었습니다! |
||
let list = []; | ||
const reverseKey = numPair[key]; | ||
|
||
if (parseInt(key) === numPair[reverseKey]) { | ||
let firstNum; | ||
let secNum; | ||
|
||
if (parseInt(key) === reverseKey) { | ||
firstNum = nums.indexOf(reverseKey); | ||
secNum = nums.indexOf(reverseKey, firstNum + 1); | ||
} else { | ||
firstNum = nums.indexOf(parseInt(key)); | ||
secNum = nums.indexOf(reverseKey); | ||
} | ||
|
||
result.push(firstNum); | ||
result.push(secNum); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
}; | ||
|
||
// TC : O(n) | ||
// 1. num 을 순회하며 numPair 객체를 만듦(num 길이 n) | ||
// 2. numPair 를 순회하며 키-값이 대칭되는 첫번째 쌍을 찾음 (numPair 길이 n) | ||
// 3. num을 순회하며 인덱스를 찾음 (num 길이 n) | ||
// O(3n) 따라서 시간복잡도는 O(n) | ||
|
||
// SC : O(n) | ||
// 크기가 n만큼인 객체(numPair)를 생성하므로 공간 복잡도도 O(n) | ||
|
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.
입력으로
nums = [0, 0], target = 0
이 주어진다면 어떨까요?[0, 1]
대신에undefined
가 반환될 것 같아서 여쭙니다.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.
앗...!!!! 네 맞습니다..!! 테스트 케이스 이외에 다른 경우를 고려하지 못했네요..
다음부터는 좀 더 세심하게 여러 케이스를 생각해서 짜야겠습니다.
피드백 감사합니다!