-
Notifications
You must be signed in to change notification settings - Fork 126
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
[jj7779607] Week3 #777
Merged
Merged
[jj7779607] Week3 #777
Changes from all commits
Commits
Show all changes
3 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 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,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const result = new Array(nums.length).fill(1); // 결과값 | ||
|
||
// 왼쪽 곱 계산 | ||
let leftProduct = 1; | ||
for (let i = 0; i < nums.length; i++) { | ||
result[i] = leftProduct; | ||
leftProduct *= nums[i]; | ||
} | ||
|
||
// 오른쪽 곱 계산해서 왼쪽 곱 계산한거 곱해주기 | ||
let rightProduct = 1; | ||
for (let i = nums.length - 1; i >= 0; i--) { | ||
result[i] *= rightProduct; | ||
rightProduct *= nums[i]; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// 조건에 O(n)이라고 있음 | ||
// 시간 복잡도: O(n) | ||
// 공간 복잡도: O(n) |
This file contains 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 @@ | ||
/** | ||
* @param {number} n - a positive integer | ||
* @return {number} - a positive integer | ||
*/ | ||
var reverseBits = function (n) { | ||
// 문자열로 변환 | ||
let nString = n.toString(2).padStart(32, "0"); | ||
//console.log(nString); | ||
|
||
// 스택 생성 (스택은 나중에 들어온게 먼저 나가므로) | ||
let stack = []; | ||
|
||
// nString 스택에 넣기 | ||
for (let i = 0; i < nString.length; i++) { | ||
stack.push(nString[i]); | ||
} | ||
|
||
// pop하여 뒤집힌 문자열 만들기 | ||
let reverseNString = ""; | ||
for (let i = 0; i < nString.length; i++) { | ||
reverseNString += stack.pop(); | ||
} | ||
|
||
// 뒤집힌 문자열을 정수로 변환 | ||
return parseInt(reverseNString, 2); | ||
}; | ||
|
||
// 시간 복잡도: O(1) | ||
// 공간 복잡도: O(1) |
This file contains 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,55 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = 0; j < nums.length; j++) { | ||
if (i !== j) { | ||
if (nums[i] + nums[j] === target) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// 처음에 풀었던 방법 -> 시간 복잡도가 O(n^2)로 nums 배열에 있는 값이 늘어날수록 성능상 좋지 못함 | ||
// 시간 복잡도: O(n^2) | ||
// 공간 복잡도: O(1) | ||
|
||
// 두 번째 푼 방법 -> 이전에 threeSum 문제 풀 때 정렬 + 포인터 이용한 것처럼 이 문제도 그런식으로 품 | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
const numsIndex = nums.map((num, i) => ({ num, i })); // 원래 인덱스 저장 | ||
//console.log(numsIndex); | ||
|
||
numsIndex.sort((a, b) => a.num - b.num); // 오름차순 정렬 | ||
//console.log(numsIndex); | ||
|
||
// left와 right 포인터 이용해 target값과 동일한 것 찾기 | ||
let left = 0; | ||
let right = numsIndex.length - 1; | ||
|
||
while (left < right) { | ||
const sum = numsIndex[left].num + numsIndex[right].num; | ||
|
||
if (sum > target) { | ||
right--; | ||
} else if (sum < target) { | ||
left++; | ||
} else { | ||
return [numsIndex[left].i, numsIndex[right].i]; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
// 첫 번째 푼 방법보다 공간 복잡도가 늘어났지만 시간 복잡도는 줄어듦 | ||
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. 👍👍 |
||
// 시간 복잡도: O(n log n) | ||
// 공간 복잡도: O(n) |
Oops, something went wrong.
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.
이렇게 하는 건 어떨까요?
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.
훨씬 간결하네요. 감사합니다!