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
23 changes: 23 additions & 0 deletions doitchuu/AddBinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
let i = a.length - 1;
let j = b.length - 1;
let carry = 0;
let result = "";

while (i >= 0 || j >= 0 || carry) {
let sum = carry;

if (i >= 0) sum += Number(a[i--]);
if (j >= 0) sum += Number(b[j--]);

result = String(sum % 2) + result;
carry = Math.floor(sum / 2);
}

return result;
};
29 changes: 29 additions & 0 deletions doitchuu/DiameterOfBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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 diameter = 0;

function dfs(node) {
if (node === null) return 0;

const left = dfs(node.left);
const right = dfs(node.right);

diameter = Math.max(diameter, left + right);

return Math.max(left, right) + 1;
}

dfs(root);
return diameter;
};
18 changes: 18 additions & 0 deletions doitchuu/MajorityElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
// 배열엔 항상 다수의 원소가 존재한다.
// 다만, n / 2개 이상인 다수의 원소를 반환한다.
const map = new Map();
const majorElement = [];

for (let i = 0; i < nums.length; i++) {
map.set(nums[i], (map.get(nums[i]) || 0) + 1);

if (map.get(nums[i]) > nums.length / 2) {
return nums[i];
}
}
};
26 changes: 26 additions & 0 deletions doitchuu/MaximumDepthOfBinaryTree.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

DFS 재귀로 최대 깊이를 구하는 정석 풀이로 깔끔하게 잘 작성하신 것 같습니다. null 처리와 Math.max(left, right) + 1 흐름도 명확해서 읽기 좋았어요.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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 maxDepth = function(root) {
function dfs(node) {
if (!node) {
return 0;
}

const left = dfs(node.left);
const right = dfs(node.right);

return Math.max(left, right) + 1;
}

return dfs(root);
};
28 changes: 28 additions & 0 deletions doitchuu/MiddleOfTheLinkedList.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

오 이렇게 Map으로 인덱스와 노드를 저장해서 middle을 찾는 방식으로도 구현할 수 있군요 알아갑니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var middleNode = function(head) {
let index = 1;
let current = head;
const map = new Map();

while (current) {
map.set(index, current);
current = current.next;
index++;
}

map.set(index, current);

let middle = Math.ceil(map.size / 2);

return map.get(middle);
};
23 changes: 23 additions & 0 deletions doitchuu/ReverseLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let prev = null;
let current = head;

while (current) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
};