Skip to content

Conversation

@hyunow
Copy link
Contributor

@hyunow hyunow commented Nov 19, 2024

๐Ÿ“Œ ํ‘ผ ๋ฌธ์ œ

๋ฌธ์ œ์ด๋ฆ„ ๋ฌธ์ œ๋งํฌ
Two Sum https://leetcode.com/problems/two-sum
Maximum Depth of Binary Tree https://leetcode.com/problems/maximum-depth-of-binary-tree
Linked List Cycle https://leetcode.com/problems/linked-list-cycle
Happy Number https://leetcode.com/problems/happy-number
Valid Parentheses https://leetcode.com/problems/valid-parentheses

๐Ÿ“ ๊ฐ„๋‹จํ•œ ํ’€์ด ๊ณผ์ •

1. Two Sum

  • ๋‹จ์ˆœํ•˜๊ฒŒ ์ด์ค‘ for๋ฌธ ํ™œ์šฉํ•ด์„œ ๋‘๊ฐœ์˜ ์ˆ˜๋ฅผ ๋”ํ•˜๋ฉด์„œ ์กฐ๊ฑด์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    const n = nums.length;
    const ans = [];

    for (let i = 0; i < n - 1; i++) {
        for (let j = i + 1; j < n; j++) {
            if (nums[i] + nums[j] === target) {
                ans.push(parseInt(i))
                ans.push(parseInt(j))
                return ans
            }
        }
    }
};

104. Maximum Depth of Binary Tree

  • ์™ผ์ชฝ ํŠธ๋ฆฌ ๊นŠ์ด์™€ ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ ๊นŠ์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ•˜๋ฉด์„œ null์ด ์•„๋‹ˆ๋ฉด +1 ์„ ํ•ด์ฃผ๋ฉด์„œ ๊นŠ์ด๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • root = null ์ธ ๊ฒฝ์šฐ๋Š”, ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ 0์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. (ํƒˆ์ถœ ์กฐ๊ฑด)
/**
 * 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) {
    if(root === null) {
        return 0
    }
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1
};

141. Linked List Cycle

  • ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋Š” slow_pointer์™€ ๋‘์นธ์”ฉ ์ด๋™ํ•˜๋Š” fast_pointer ๋‘๊ฐœ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • cycle์ด ์ƒ๊ธฐ๋ฉด slow_pointer์™€ fast_pointer๊ฐ€ ๋™์ผํ•ด์ง€๋Š” ์ˆœ๊ฐ„์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    var slow_pointer = head
    var fast_pointer = head

    while(fast_pointer !== null && fast_pointer.next !== null) {
        slow_pointer = slow_pointer.next
        fast_pointer = fast_pointer.next.next
        if(slow_pointer === fast_pointer) {
            return true
        }
    }

    return false
};

20. Valid Parentheses

  • stack์„ ํ™œ์šฉํ•ด์„œ stack์˜ ๊ฐ€์žฅ ์œ—๋ถ€๋ถ„ ๊ด„ํ˜ธ์™€ s์˜ ํ˜„์žฌ ๊ด„ํ˜ธ๊ฐ€ ์Œ์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์Œ์ด ๋งž์œผ๋ฉด stack.pop์„, ๋งž์ง€ ์•Š์œผ๋ฉด s์˜ ํ˜„์žฌ ๊ด„ํ˜ธ๋ฅผ stack์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
  • if ๋ฌธ์„ ์ข€ ๋ฉ์ฒญํ•˜๊ฒŒ ์“ด ๊ฒƒ ๊ฐ™๊ธด ํ•ฉ๋‹ˆ๋‹ค... ><
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    const stack = [];
    const n = s.length;
    for (let i = 0; i < n; i++) {
        if (!stack) {
            stack.push(s[i])
        } else {
            let temp = stack.pop()
            if( (temp === '(' && s[i] === ')') ||
                (temp === '{' && s[i] === '}') ||
                (temp === '[' && s[i] === ']') ) {
                    continue
            } else {
                stack.push(temp)
                stack.push(s[i])
            }
        }
    }
    console.log(stack)
    if(stack.length === 1) {
        return true
    } else {
        return false
    }
};

202. Happy Number

  • set์„ ํ™œ์šฉํ•ด์„œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ˆซ์ž๋ฅผ ํ™•์ธํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ์ž๋ฆฟ์ˆ˜ ์ œ๊ณฑ์˜ ํ•ฉ์ด 1์ด ์•„๋‹ˆ๋ฉด set์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’์ด๋ฉด return false, ์ฒ˜์Œ ๋ณด๋Š” ๊ฐ’์ด๋ฉด set์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
    const seen = new Set()

    while(true) {
        let sum = 0
        while (n > 0) {
            const num = n % 10
            sum += num ** 2
            n = Math.floor(n / 10)
        }
    
        if(sum === 1) {
            return true
        }

        if(seen.has(sum)) {
            return false
        }
    
        seen.add(sum)
        n = sum
        
    }
    
};

@hyunow hyunow self-assigned this Nov 19, 2024
@oris8 oris8 requested review from FrogBaek and JooKangsan November 19, 2024 14:02
Copy link
Contributor

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

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

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Copy link
Contributor

Choose a reason for hiding this comment

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

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!! ํ˜„์žฌ ์ž‘์„ฑ๋œ ์ฝ”๋“œ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(nยฒ)์ธ๋ฐ์š” ํ•ด์‹œ๋งต์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ O(n)์œผ๋กœ ๊ฐœ์„  ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ€๋” ๋ณด๋Ÿฌ ๊ฐ€๋Š”๋ฐ์š” https://github.com/trekhleb/javascript-algorithms/blob/master/README.ko-KR.md
์ด๊ฑฐ ํ•œ๊ตญ์–ด๋กœ ๋ณ€์—ญ๋„ ๋˜์–ด์žˆ์–ด์„œ ์ฐธ๊ณ  ํ• ๋งŒ ํ• ๊ฑฐ์—์š”!

@oris8 oris8 merged commit 5931cd7 into master Nov 26, 2024
JooKangsan pushed a commit that referenced this pull request Dec 3, 2024
* 1 / Two Sum / Easy / 8m

* 104 / Maximum Depth of Binary Tree / Easy / 11m

* 141 / Linked List Cycle / Easy / 32m

* 20 / Valid Parentheses / Easy / 11m

* 202 / Happy Number / Easy / 23m

---------

Co-authored-by: user <user@cseui-MacBookPro.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants