-
-
Notifications
You must be signed in to change notification settings - Fork 245
[나리] WEEK 03 Solutions #386
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 1 commit
Commits
Show all changes
5 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[]} | ||
*/ | ||
|
||
/** | ||
* Runtime: 107ms, Memory: 49.52MB | ||
* Time complexity: O(n^2) | ||
* Space complexity: O(n^2) | ||
* | ||
* **/ | ||
|
||
var twoSum = function (nums, target) { | ||
const answer = []; | ||
for (let i = 0; i < nums.length - 1; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] === target) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
|
||
/** | ||
* 다른 풀이: two의 sum 이니까 target - 현재 값을 한 뒤, 나머지 값이 배열에 있는지 찾는 방법. | ||
* 이 과정에서 해시맵을 사용해서 target - 현재 값이 map 에 존재하면 return | ||
* 없다면 map에 나머지 값과 그에 해당하는 인덱스를 추가한다. | ||
* 그럼 추후에는 나머지 값을 map에서 O(1)로 찾기만 하면 된다. | ||
* | ||
* Runtime: 49ms, Memory: 49.52MB | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* **/ | ||
|
||
var twoSum = function (nums, target) { | ||
let map = new Map(); | ||
|
||
for (i = 0; i < nums.length; i++) { | ||
if (map.has(target - nums[i])) return [map.get(target - nums[i]), i]; | ||
map.set(nums[i], i); | ||
} | ||
}; |
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.
프론트엔드 엔지니어로 일 할 땐
Map
이 있다는 것만 알고 한 번도 써본 적이 없었는데 나리님께서 풀이에 사용하셨길래 좀 찾아보았습니다(key, value) 쌍을 담는 컨테이너 역할로 사용하기엔
Map
이 더 편한 구석이 많다는 것 알아갑니다 :DThere 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.
저도 현업에서는 아직 써본 적은 없는데, 이번 기회로 필요하다면 써봐야겠다는 생각을 했습니다! 감사합니다~