Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-02 #317

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-02

SaraadKun commented 2 years ago

622. 设计循环队列

class MyCircularQueue {

    int[] q;
    int N, head, tail, size;
    public MyCircularQueue(int k) {
        this.N = k;
        this.q = new int[N];
        this.head = 0;
        this.tail = 0;
        this.size = 0;
    }

    public boolean enQueue(int value) {
        if (isFull()) { 
            return false;
        }
        if (!isEmpty()) {
            tail = (tail + 1) % N;
        }
        q[tail] = value;
        size++;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) { 
            return false;
        }
        size--;
        if (!isEmpty()) {
            head = (head + 1) % N;
        }
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return q[head];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return q[tail];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == N;
    }
}

WeChat: Saraad

restartgr commented 2 years ago
/*
 * @lc app=leetcode.cn id=112 lang=javascript
 *
 * [112] 路径总和
 */

// @lc code=start
/**
 * 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
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function (root, targetSum) {
    if (!root) {
        return false
    }
    const search = (node, count) => {
        if (!node.left && !node.right && count === 0) {
            return true
        }
        if (!node.left && !node.right) {
            return false
        }
        if (node.left) {
            if (search(node.left, count - node.left.val)) return true
        }
        if (node.right) {
            if (search(node.right, count - node.right.val)) return true
        }
        return false
    }
    return search(root, targetSum - root.val)
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

restartgr commented 2 years ago
/*
 * @lc app=leetcode.cn id=113 lang=javascript
 *
 * [113] 路径总和 II
 */

// @lc code=start
/**
 * 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
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function (root, targetSum) {
    const res = []
    const search = (node, count, path) => {
        if (count === 0 && !node.left && !node.right) {
            res.push([...path])
            return
        }
        if (!node.left && !node.right) {
            return
        }
        if (node.left) {
            path.push(node.left.val)
            search(node.left, count - node.left.val, path)
            path.pop()
        }
        if (node.right) {
            path.push(node.right.val)
            search(node.right, count - node.right.val, path)
            path.pop()
        }
    }
    if (!root) {
        return []
    }
    search(root, targetSum - root.val, [root.val])
    return res
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

restartgr commented 2 years ago

剑指 Offer 04. 二维数组中的查找


/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var findNumberIn2DArray = function (matrix, target) {
    if (!matrix || !matrix.length) {
        return false
    }
    const m = matrix.length
    const n = matrix[0].length
    let row = 0
    let col = n - 1
    while (col >= 0 && row < m) {
        if (matrix[row][col] > target) {
            col--
        } else if (matrix[row][col] < target) {
            row++
        } else {
            return true
        }
    }
    return false

};

Jörmungandr

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=121 lang=typescript
 *
 * [121] Best Time to Buy and Sell Stock
 */

// @lc code=start
function maxProfit(prices: number[]): number {
    let max = 0;
    let l = 0;
    let r = 1;

    while (r <= prices.length - 1) {
        // if left price is less than right
        // then compare max and difference between prices[l] and prices[r]
        // make the max equals to the bigger one
        if (prices[l] < prices[r]) {
            max = Math.max(max, prices[r] - prices[l]);
        // l is always the smallest one
        } else {
            l = r;
        }
        r++;
    }

    return max;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件