Skip to main content

· One min read

https://leetcode.com/problems/linked-list-cycle/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
if (head === null) {
return false;
}

let curr = head;
let next = head.next;

while (next && next.next) {
curr = curr.next;
next = next.next.next;

if (curr === next) {
return true;
}
}

return false;
};

· One min read

https://leetcode.com/problems/binary-tree-preorder-traversal/

Solution 1

/**
* 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 preorderTraversal = function (root) {
let result = [];

traversalHelper(root, result);

return result;
};

const traversalHelper = (root, result) => {
if (root === null) {
return;
}
result.push(root.val);
traversalHelper(root.left, result);
traversalHelper(root.right, result);
};

· One min read

https://leetcode.com/problems/binary-tree-postorder-traversal/

Solution 1

/**
* 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 postorderTraversal = function (root) {
let result = [];
traversalHelper(root, result);
return result;
};

const traversalHelper = (root, result) => {
if (root === null) {
return;
}
result.unshift(root.val);
traversalHelper(root.right, result);
traversalHelper(root.left, result);
};

· One min read

https://leetcode.com/problems/intersection-of-two-linked-lists/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function (headA, headB) {
if (!headA || !headB) {
return null;
}

let nodeA = headA;
let nodeB = headB;

while (nodeA !== nodeB) {
nodeA = nodeA.next;
nodeB = nodeB.next;

console.log(nodeA, nodeB);

if (nodeA === nodeB) {
return nodeA;
}

if (!nodeA) {
nodeA = headB;
}
if (!nodeB) {
nodeB = headA;
}
}

return nodeA;
};

· One min read

https://leetcode.com/problems/excel-sheet-column-title/

Solution 1

/**
* @param {number} columnNumber
* @return {string}
*/
var convertToTitle = function (columnNumber) {
// String.fromCharCode(65) === "A"
// 한 자릿수일 경우
if (columnNumber - 1 < 26) {
return String.fromCharCode(65 + ((columnNumber - 1) % 26));
}

// 두 자릿수 이상일 경우
let result = '';
while (columnNumber > 0) {
const remainder = (columnNumber - 1) % 26;
const remainderCharCode = String.fromCharCode(65 + remainder);
result = remainderCharCode + result;
columnNumber = parseInt((columnNumber - 1) / 26);
}
return result;
};

다시 봐야함

· One min read

https://leetcode.com/problems/majority-element/

Solution 1

/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function (nums) {
const element = {};

for (const num of nums) {
if (element[num]) {
element[num]++;
} else {
element[num] = 1;
}
}

let max = 0;
let targetNum = '';

for (let e in element) {
if (element[e] > max) {
max = element[e];
targetNum = e;
}
}

return Number(targetNum);
};

· One min read

https://leetcode.com/problems/excel-sheet-column-number/

Solution 1

/**
* @param {string} columnTitle
* @return {number}
*/
var titleToNumber = function (columnTitle) {
if (columnTitle.length === 1) {
return (columnTitle.charCodeAt(0) % 65) + 1;
}

let result = 0;

for (let i = columnTitle.length - 1; i >= 0; i--) {
const strCode = (columnTitle[i].charCodeAt(0) % 65) + 1;
if (i === columnTitle.length - 1) {
result += strCode;
} else {
result += strCode * 26 ** (columnTitle.length - i - 1);
}
}
return result;
};

· One min read

https://leetcode.com/problems/valid-parentheses/

Solution 1

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
if (s.length % 2 !== 0) {
return false;
}

const stack = [];
const brackets = {
'(': ')',
'{': '}',
'[': ']',
};

for (let i = 0; i < s.length; i++) {
if (Object.keys(brackets).includes(s[i])) {
stack.push(s[i]);
} else {
if (brackets[stack[stack.length - 1]] === s[i]) {
stack.pop();
} else {
return false;
}
}
}
return stack.length === 0;
};

Runtime: 240 ms

Memory Usage: 51.4 MB

· One min read

https://leetcode.com/problems/happy-number/

Solution 1

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function (n) {
let set = new Set();

while (!set.has(n)) {
set.add(n);
let s = n.toString();
n = 0;
for (let i = 0; i < s.length; ++i) {
n += squares[s[i]];
}
if (n === 1) return true;
}

return false;
};

const squares = {
0: 0,
1: 1,
2: 4,
3: 9,
4: 16,
5: 25,
6: 36,
7: 49,
8: 64,
9: 81,
};