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

[나리] WEEK04 Solutions #424

Merged
merged 3 commits into from
Sep 8, 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
62 changes: 62 additions & 0 deletions missing-number/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @param {number[]} nums
* @return {number}
*/

/**
* Runtime: 63ms, Memory: 51.68MB
* Time complexity: O(nlogn)
* Space complexity: O(nlogn)
*
*/

var missingNumber = function (nums) {
const n = nums.length;
nums.sort((a, b) => a - b);

if (!nums.includes(0)) {
return 0;
}
for (let i = 0; i < n; i++) {
if (nums[i + 1] - nums[i] !== 1) {
return nums[i] + 1;
}
}
return nums[-1];
};

/**
* NOTE
* if use 'sort()' -> O(nlogn)
* if you solve this problem without using sort(), can use sum of nums
*/

var missingNumber = function (nums) {
const sumOfNums = nums.reduce((num, total) => num + total, 0);

const n = nums.length;
const expectedSum = (n * (n + 1)) / 2;

if (expectedSum === sumOfNums) {
return 0;
} else {
return expectedSum - sumOfNums;
}
};

/**
* NOTE
* or you can subtract while adding
*/

var missingNumber = function (nums) {
let target = 0;
for (let i = 0; i <= nums.length; i++) {
target += i;

if (i < nums.length) {
target -= nums[i];
}
}
return target;
};
29 changes: 29 additions & 0 deletions valid-palindrome/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {string} s
* @return {boolean}
*/

/**
* Runtime: 66ms, Memory: 54.75MB
* Time complexity: O(s.length)
* Space complexity: O(s.length)
*
*/

var isPalindrome = function (s) {
let trimmed = s.toLowerCase();
let answer = [];
let checkAlphabet = /[a-zA-Z]/;
let checkNum = /[0-9]/;
Comment on lines +14 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let trimmed = s.toLowerCase();
let answer = [];
let checkAlphabet = /[a-zA-Z]/;
let checkNum = /[0-9]/;
const trimmed = s.toLowerCase();
const answer = [];
const checkAlphabet = /[a-zA-Z]/;
const checkNum = /[0-9]/;

재할당되지 않은 변수를 const 키워드로 선언하는 것을 추천합니다!
가독성과 이후 의도지 않은 버그 발생 여지 사단 차단한다는 점에서 장점을 가지고 있어요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이전 로직에서 재할당하려고 let으로 작성했는데 바꾸는걸 까먹었군요 ㅎㅎ 감사합니다!


for (let alpha of trimmed) {
if (checkAlphabet.test(alpha) || checkNum.test(alpha)) {
answer.push(alpha);
}
}

if (answer.join("") === answer.reverse().join("")) {
return true;
}
return false;
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 작성할 수도 있겠네요!

Suggested change
if (answer.join("") === answer.reverse().join("")) {
return true;
}
return false;
return answer.join("") === answer.reverse().join(""))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이게 더 간단하겠네요! 👍

};