堆-深度-广度-leetcode

163 阅读2分钟

有效的括号 20题

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。

示例 1:

输入: "()" 输出: true

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    let arr = s.split('');
    let tmp = [];
    arr.forEach(i => {
        let item = tmp[tmp.length - 1];
        switch(i) {
            case '(':
            case '{':
            case '[':
                tmp.push(i);
                break;
            case ')':
                if (item === '(') {
                    tmp.pop();
                    break;
                } else {
                    tmp.push(i);
                }
            case ']':
                if (item === '[') {
                    tmp.pop();
                    break;
                } else {
                    tmp.push(i);
                }
            case '}':
                if (item === '{') {
                    tmp.pop();
                    break;
                } else {
                    tmp.push(i);
                }
            default:
                break;
        }
    });
    return !tmp.length
};
每日温度 739

请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。

例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。

提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。

/**
 * @param {number[]} T
 * @return {number[]}
 */
var dailyTemperatures = function(T) {
    let res = T.map(i => 0);
    let st = [];
    for (let i = 0; i < T.length; i++) {
        while (st.length > 0 && T[i] > T[st[st.length -1]]) {
            let t = st.pop();
            res[t] = i - t;
        }
        st.push(i);
    }
    return res;
};
队列实现栈 255
/**
 * Initialize your data structure here.
 */
var MyStack = function() {
    this.stack = [];
};

/**
 * Push element x onto stack. 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function(x) {
    this.stack[this.stack.length] = x;
    return this.stack.length;
};

/**
 * Removes the element on top of the stack and returns that element.
 * @return {number}
 */
MyStack.prototype.pop = function() {
    if (this.stack.length < 1) {
        return undefined;
    }
    let popEle = this.stack[this.stack.length-1];
    this.stack.length = this.stack.length - 1;
    return popEle;
};

/**
 * Get the top element.
 * @return {number}
 */
MyStack.prototype.top = function() {
    if (this.stack.length < 1) {
        return undefined;
    }
    return this.stack[this.stack.length - 1];
};

/**
 * Returns whether the stack is empty.
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return this.stack.length === 0
};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */
删除所有重复元素 82

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5 输出: 1->2->5

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    let dummy = new ListNode(-1);
    dummy.next = head;
    let slow = dummy,
    fast = dummy.next;
    while(fast) {
        while (fast.next && fast.next.val === fast.val) {
            fast = fast.next;
        }
        if (slow.next === fast) {
            slow = slow.next;
        } else {
            slow.next = fast.next;
        }
        fast = fast.next;
    }
    return dummy.next;
};