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
27 changes: 27 additions & 0 deletions doitchuu/BinarySearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function search(nums, target) {
return nums.indexOf(target);
}

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {
let start = 0;
let end = nums.length - 1;

while (start <= end) {
const middle = Math.floor((start + end) / 2);

if (nums[middle] === target) {
return middle;
} else if (nums[middle] > target) {
end = middle - 1;
} else {
start = middle + 1;
}
}

return -1;
};
30 changes: 30 additions & 0 deletions doitchuu/FloodFill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var floodFill = function (image, sr, sc, color) {
const dx = [-1, 0, 1, 0];
const dy = [0, 1, 0, -1];

const original = image[sr][sc];
if (original === color) return image;

const rows = image.length;
const cols = image[0].length;

const queue = [[sr, sc]];
image[sr][sc] = color; // 방문 처리(색칠) 먼저

while (queue.length) {
const [x, y] = queue.shift();

for (let dir = 0; dir < 4; dir++) {
const nx = x + dx[dir];
const ny = y + dy[dir];

if (nx < 0 || ny < 0 || nx >= rows || ny >= cols) continue;
if (image[nx][ny] !== original) continue;

image[nx][ny] = color;
queue.push([nx, ny]);
}
}

return image;
};
12 changes: 12 additions & 0 deletions doitchuu/InvertBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function invertTree(root) {
if (root === null) return null;

const temp = root.left;
root.left = root.right;
root.right = temp;

invertTree(root.left);
invertTree(root.right);

return root;
}
32 changes: 32 additions & 0 deletions doitchuu/ValidAnagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) {
return false;
}

const sMap = new Map();
const tMap = new Map();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Map을 1개만 두고 +1, -1하면서 연산하면 코드량 감소, 메모리 절약 효과도 있을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

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

지현님 풀이도 좋군요! 피드백 감사합니다

for (let i = 0; i < s.length; i++) {
sMap.set(s[i], (sMap.get(s[i]) ?? 0) + 1);
tMap.set(t[i], (tMap.get(t[i]) ?? 0) + 1);
}

for (const [key, value] of sMap) {
const tValue = tMap.get(key);

if (tValue === undefined) {
return false;
}

if (tValue !== value) {
return false;
}
}

return true;
};