diff --git a/product-of-array-except-self/higeuni.js b/product-of-array-except-self/higeuni.js new file mode 100644 index 000000000..3c8a8417b --- /dev/null +++ b/product-of-array-except-self/higeuni.js @@ -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 +}; + diff --git a/reverse-bits/higeuni.js b/reverse-bits/higeuni.js new file mode 100644 index 000000000..51cb54c04 --- /dev/null +++ b/reverse-bits/higeuni.js @@ -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; +} + diff --git a/two-sum/higeuni.js b/two-sum/higeuni.js new file mode 100644 index 000000000..466f99ddd --- /dev/null +++ b/two-sum/higeuni.js @@ -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); + } +}; +