Skip to content

Commit

Permalink
Feat: Week 3
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGeuni committed Dec 28, 2024
1 parent 736ee84 commit 35832fc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
24 changes: 24 additions & 0 deletions product-of-array-except-self/higeuni.js
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
};

36 changes: 36 additions & 0 deletions reverse-bits/higeuni.js
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;
}

22 changes: 22 additions & 0 deletions two-sum/higeuni.js
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);
}
};

0 comments on commit 35832fc

Please sign in to comment.