Skip to content
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

[khyo] Week3 #802

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 방식으로 쓰는 건 생각 못해봤네요. 인사이트 얻고 갑니다 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다~ 이번주도 고생많으셨습니다~!@

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);
}
};

Loading