📣 VIP通行证夏日特惠 限时立减$68
楼主: sicilianee
跳转到指定楼层
上一主题 下一主题
收起左侧

记录

🔗
 楼主| sicilianee 2017-11-3 11:49:31 | 只看该作者
全局:
1102 3. Valid Parentheses

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
    // !!!!! you will use stack, not just a counter, to process multiple ones
    if (s == null || s.length === 0) {
        return true;
    }
    const stack = [];
    for (let c of s) {
        if (['(', '{', '['].includes(c)) {
            stack.push(c);
        } else {
            if (stack.length === 0) {return false;} // !!! have to guard it !!!
            const ac = stack.pop();
            // !!!!! there is an || you mistake to && , careful about long exps !!!1
            if ((c === '}') && (ac === '{') || (c === ']') && (ac === '[') || c === ')' && ac === '(') {
                // nothing
            } else {
                return false;
            }
        }
    }
    return stack.length === 0;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-3 12:00:14 | 只看该作者
全局:
1102 4. Remove Nth Node From End of List

/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
    if (head == null) {
        return head;
    }
    const dummy = new ListNode(0); // !!!! why? you might be asked to remove the first node from the list
    dummy.next = head;
    let slow = dummy;
    let fast = dummy;
    for (let i = 0; i < n; i++) {
        fast = fast.next;
    }
    while (fast.next != null) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return dummy.next;
};

Still need the f***ing dummy node !!1
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 02:15:44 | 只看该作者
全局:
1105 1. Count of Smaller Numbers After Self

/**
* @param {number[]} nums
* @return {number[]}
*/
var countSmaller = function(nums) {
    // check
    // sort and de-duplicate
    // map values to their indice + 1
    // create the dict
    // loop the nums reversely, get mapping value and add it to the tree, and get sum for each one added
    if (nums == null || nums.length === 0) {
        return []; // !!! not empty
    }
    const dict = {};
    const allNums = ([...new Set(nums)]).sort((a, b) => a - b);
    ;allNums.map((value, index) => {
        dict[value] = index + 1;
    });
    const tree = new FenwickTree(allNums.length); // we do + 1 in the tree
    const res = [];
    console.log('to start with');
    console.log(tree.c);
    for (let i = nums.length - 1; i >= 0; i--) {
        const newValue = dict[nums[i]];
        res[i] = tree.sum(newValue - 1); // !!! on the right, not including itself !!!! If value - 1 is 0, it will return 0;
        tree.add(newValue, 1); // !!!! when you add, you also need to specify index !!! The index is the value, and the value is the count 1
    }
    return res;
   
};

// class, no ()
class FenwickTree  {
    constructor (n) {
        this.n = n;
        this.c = Array(n + 1).fill(0); // !!! will need to init  it
    }
    add (x, val) {
        while (x <= this.n) {
            this.c[x] += val;
            x += FenwickTree.lowBit(x);
        }
    }
    sum (x) {
        let res = 0;
        while (x >= 1) { // !!!! Could be 1
            res += this.c[x];
            x -= FenwickTree.lowBit(x);
        }
        return res;
    }
    static lowBit (x) {
        return x & (-x);
    }
}

Need to do it again in one shoot.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 02:33:25 | 只看该作者
全局:
1105 2. Minimum Depth of Binary Tree

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var minDepth = function(root) {
    // !!! the problem is that null node doesn't count
    if (root == null) {
        return 0;
    }
    if (root.left == null && root.right == null) {  // note: seems like you don't need this one
        return 1;
    }
    // !!! the key thing is that we cannot enter with a null value
    if (root.left == null) {return minDepth(root.right) + 1}
    if (root.right == null) {return minDepth(root.left) + 1};
    return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 02:48:21 | 只看该作者
全局:
1105 3. Implement Stack using Queues

/**
* Initialize your data structure here.
*/
var MyStack = function() {
    this.mainQ = [];
    this.tmpQ = [];
};

/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
    while (this.mainQ.length > 0) {  // !!! keep forgetting `this`
        this.tmpQ.push(this.mainQ.shift());
    }
    this.mainQ.push(x);
    while (this.tmpQ.length > 0) {
        this.mainQ.push(this.tmpQ.shift());
    }
};

/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
    return this.mainQ.shift();
};

/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
    return this.mainQ[0];
};

/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
    return this.mainQ.length <= 0   // !!!! test if is empty, not if is not empty, you did too much invalid checking
};

/**
* Your MyStack object will be instantiated and called as such:
* var obj = Object.create(MyStack).createNew()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 02:52:47 | 只看该作者
全局:
sicilianee 发表于 2017-11-6 02:48
1105 3. Implement Stack using Queues

/**

also check other solutions:

https://leetcode.com/articles/implement-stack-using-queues/
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 03:43:23 | 只看该作者
全局:
1105 4. Permutations II

/**
* @param {number[]} nums
* @return {number[][]}
*/
var permuteUnique = function(nums) {
    if (nums == null || nums.length === 0) {
        return [];
    }
    const res = [];
    recurse(0);
    return res;
   
    function recurse (start) {
        if (start === nums.length) {   
            res.push([...nums]);
            return;
        }
        for (let i = start; i < nums.length; i++) { // !!! no equal to length, why you do that
            // !!!! could be start itself, becuase we need to give chance to itself !!!
            if (i !== start && noSwap(nums, start, i)) { // !!! but here you need to exclude start since we need it
                continue;
            }
            swap(nums, start, i);
            recurse(start + 1);
            swap(nums, start, i);
        }
    }
};

function noSwap (nums, start, i) {
    for (let k = start; k < i; k++) { // !!!! it is k++> always the same issue. first it is j, then here it is k++ not i++
        if (nums[k] === nums[i]) {
            return true;
        }
    }
    return false; // !!! true and false reversed
}
               

function swap (nums, i, j) {
    ;[nums[i], nums[j]] = [nums[j], nums[i]];
}
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 07:25:12 | 只看该作者
全局:
1105 5. Rotate Function

/**
* @param {number[]} A
* @return {number}
*/
var maxRotateFunction = function(A) {
    if (A == null || A.length === 0) {
        return 0;
    }
    let value = 0; // !!!!! should init it !!!!!
    const len = A.length;
    for (let i = 0; i < len; i++) {
        value = value + i * A[i];
    }
    console.log(value);
    let max = value;
    const sum = A.reduce((a, b) => a + b);
    for (let i = 1; i < len; i++) {
        value = value + sum - len * A[len - i];
        max = Math.max(value, max);
    }
    return max;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 07:37:02 | 只看该作者
全局:
1105 6.  Insertion Sort List

/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var insertionSortList = function(head) {
    if (head == null) {
        return head;
    }
    const dummy = new ListNode(0);
    let node = head;
    while (node != null) {
        const next = node.next;
        let prev = dummy;
        while (prev.next != null && prev.next.val <= node.val) {
            prev = prev.next;
        }
        // !!!! these are not inside the inner loop !!!
        const prevNext = prev.next;
        prev.next = node;
        node.next = prevNext;
        node = next;
    }
    return dummy.next;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-6 08:04:11 | 只看该作者
全局:
1105 7. H-Index

/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function(citations) {
    if (citations == null || citations.length === 0) {
        return 0;
    }
    citations.sort((a, b) => b - a); // !!! reverse
    // !!!!
    for (let i = 0; i < citations.length; i++) {
        if (i >= citations[i]) { // !!! has equal? This is important and tricky !!!
            return i;
        }
    }
    return citations.length;
};
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表