-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
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,24 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
* | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
var productExceptSelf = function(nums) { | ||
const answer = [] | ||
const zeros = nums.filter(n => n === 0).length; | ||
if (zeros > 1) return new Array(nums.length).fill(0); | ||
|
||
const productOfNums = nums.reduce((acc, cur) => cur === 0 ? acc : acc * cur, 1); | ||
|
||
nums.forEach(num => { | ||
if (num === 0) { | ||
answer.push(productOfNums); | ||
} else { | ||
answer.push(zeros ? 0 : productOfNums / num); | ||
} | ||
}); | ||
return answer | ||
}; | ||
|
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,36 @@ | ||
/** | ||
* @param {number} n - a positive integer | ||
* @return {number} - a positive integer | ||
* | ||
* Time Complexity: O(n)? n이 32이기 때문에 O(1)? | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
// 일반적인 풀이 | ||
var reverseBits = function(n) { | ||
const binary = n.toString(2); | ||
const reversedBinary = binary.split('').reverse().join('').padEnd(32, '0'); | ||
const answer = parseInt(reversedBinary, 2); | ||
|
||
return answer; | ||
// return parseInt(n.toString(2).split('').reverse().join('').padEnd(32, '0'), 2); | ||
}; | ||
|
||
/** | ||
* | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
// 비트 연산을 이용한 풀이 | ||
var reverseBits2 = function(n) { | ||
let result = 0; | ||
for(let i = 0; i < 32; i++) { | ||
// result를 왼쪽으로 시프트하고 n의 마지막 비트를 더함 | ||
result = (result << 1) | (n & 1); | ||
// n을 오른쪽으로 시프트 | ||
n = n >> 1; | ||
} | ||
return result >>> 0; | ||
} | ||
|
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,22 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
* | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
var twoSum = function(nums, target) { | ||
const map = new Map(); | ||
|
||
for (let i = 0; i < nums.length; ++i) { | ||
const diff = target - nums[i]; | ||
|
||
if (map.has(diff)) { | ||
return [map.get(diff), i]; | ||
} | ||
|
||
map.set(nums[i], i); | ||
} | ||
}; | ||
|