Skip to content
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
55 changes: 55 additions & 0 deletions raejun/AddBinary.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

전 조건으로 처리하려다가 점점 꼬여서 결국 못풀었는데 잘하시네요! a.at(-i), b.at(-i)를 활용해서 길이가 다른 경우도 자연스럽게 처리한 부분 좋은것 같습니다. 다만 분기가 조금 많은 편이라 aBit, bBit, sum처럼 중간 변수를 두면 로직이 더 읽기 쉬워질 것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function (a, b) {
const aLen = a.length;
const bLen = b.length;
const len = aLen > bLen ? aLen : bLen;
let num = 0;
const arr = [];

for (let i = 1; i <= len; i++) {
if (a.at(-i) === "1" && b.at(-i) === "1") {
arr.push(0 + num);
num = 1;
} else if (a.at(-i) === "1" || b.at(-i) === "1") {
if (num === 1) {
arr.push(0);
num = 1;
} else {
arr.push(1);
num = 0;
}
} else {
if (num === 1) {
arr.push(1);
num = 0;
} else {
arr.push(0);
num = 0;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

요런 예외케이스 처리를 잘하시는군요 👍 !!
나중에 조건식을 변수로 빼도 좋을 것 같아요 얼마전에 라이브코테 관련 유튜브를 봤는데
코드 가독성 부분도 신경쓴다는 걸 본거 같아서 풀이가 좋으니 중간중간 더 읽기 좋아지면 더 좋은 풀이가 될 것 같아요 💯


return (num === 1 ? "1" : "") + arr.reverse().join("");
};

/*
26분 걸림.

시간 복잡도는 O(n)이다.

문자열을 뒤에서부터 순회하면서 풀이했다.
a와 b의 길이를 구해서, 더 긴 문자열의 길이만큼 루프를 돌면서 풀이했다.
a와 b의 각 자리 숫자를 비교하면서, 1과 1이 나오면 0을 arr에 push하고 num을 1로 업데이트했다.
1과 0이 나오면, num이 1이면 arr에 0을 push하고 num을 1로 업데이트했다.
num이 0이면 arr에 1을 push하고 num을 0으로 업데이트했다.
0과 0이 나오면, num이 1이면 arr에 1을 push하고 num을 0으로 업데이트했다.
num이 0이면 arr에 0을 push하고 num을 0으로 업데이트했다.
루프가 끝난 후에, num이 1이면 "1"을 반환값의 앞에 붙이고, arr를 뒤집어서 문자열로 만들어서 반환한다.

풀이가 생각보다 어려웠다. 십진수를 이진수로 이진수를 십진수로 변환하는 함수를 만들어서 풀어도 될 것 같다.
배열에 push하는 방식말고 뭔가 다른 방식으로 풀이할 수 있을 것 같은데 생각이 나지 않는다.
*/
50 changes: 50 additions & 0 deletions raejun/DiameterofBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var diameterOfBinaryTree = function (root) {
let maxLevel = 0;

const dfs = (node, level) => {
if (!node) {
if (maxLevel < level) maxLevel = level;

return;
}

if (node.left) dfs(node.left, level + 1);
if (node.right) dfs(node.right, level + 1);
};

dfs(root.right, 0);

const tmp = maxLevel;
maxLevel = 0;

dfs(root.left, 0);

return tmp + maxLevel;
};

/*
문제 풀지 못함.

시간 복잡도는 O(n)이다.

DFS 알고리즘을 이용하여 풀이했다.

DFS를 이용해서 문제를 풀려고 했다.
DFS를 왼쪽 오른쪽을 구분해서 풀려고 했다.
접근이 완전히 잘못됐다.
한쪽으로만 DFS가 될수도 있는 상황이다.
사실 양쪽으로 나눠서 구하는 방식도 제대로 구현하지 못했다.

*/
Loading