楼主: sicilianee
跳转到指定楼层
上一主题 下一主题
收起左侧

记录

🔗
 楼主| sicilianee 2017-10-15 07:42:12 | 只看该作者
全局:
1013 1. Symmetric Tree

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
    if (root == null) {return true;}
    return recurse(root.left, root.right);
   
    function recurse (left, right) {
        if (left == null && right == null) {return true;}
        if (left == null || right == null) {return false;}
        if (left.val != right.val) {return false;}
        const leftRes = recurse(left.left, right.right);
        const rightRes = recurse(left.right, right.left);
        return leftRes && rightRes;
    }
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 08:23:31 | 只看该作者
全局:
1013 2. Solve the Equation

/**
* @param {string} equation
* @return {string}
*/
var solveEquation = function(equation) {
    const [left, right] = equation.split('=');
    const [xCoeLeft, numCoeLeft] = parse(left);
    const [xCoeRight, numCoeRight] = parse(right);
    const xCoe = xCoeLeft - xCoeRight;
    const numCoe = numCoeRight - numCoeLeft;
    if (xCoe === 0) {
        return numCoe === 0 ? 'Infinite solutions' : 'No solution';
    } else {
        return `x=${numCoe / xCoe}`;
    }
   
   
    function parse (exp) {
        const operators = exp.split('').filter(c => ['+', '-'].includes(c)).map(c => c === '+' ? 1 : -1);
        const operands = exp.split(/\+|-/).filter(c => c); // !!! remove empty ones
        if (operators.length !== operands.length) { // !!!!! canot decide by if first one is -1, cause second one could be -1
            operators.unshift(1); // !!!! here it is already number, not string any more!!! !!! unshift not push !!!!!
        }
        let xCoe = 0;
        let numCoe = 0;
        for (let i = 0; i < operands.length; i++) {
            const operand = operands[i];
            if (operand[operand.length - 1] === 'x') {
                let coe = 0;
                if (operand === 'x') {
                    coe = 1;
                } else {
                    coe = Number.parseInt(operand.slice(0, operand.length - 1));
                }
                xCoe += coe * operators[i];
            } else {
                const coe = Number.parseInt(operand, 10);
                numCoe += coe * operators[i];
            }
        }
        return [xCoe, numCoe];
    }
};


Too many details.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 08:33:01 | 只看该作者
全局:
1014 1. Pascal's Triangle

/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
    if (numRows < 1) {return [];}
    const res = [[1]];
    for (let i = 1; i < numRows; i++) {
        const list = [];
        for (let j = 0; j < i + 1; j++) {
            const prev = j - 1 >= 0 ? res[i - 1][j - 1] : 0;
            const next = j < i ? res[i - 1][j] : 0
            list[j] = prev + next;
        }
        res.push(list);
    }
    return res;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 09:26:49 | 只看该作者
全局:
1014 2.  Ones and Zeroes

/**
* @param {string[]} strs
* @param {number} m
* @param {number} n
* @return {number}
*/
// 10, 0001, 111001, 1, 0  ...  5, 3
var findMaxForm = function(strs, m, n) {
    if (strs == null || strs.length === 0) {return 0;}
    const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(0));
    for (let str of strs) {
        const [zero, one] = count(str);
        for (let i = m; i >= zero; i--) {
            for (let j = n; j >= one; j--) {
                dp[i][j] = Math.max(dp[i][j], dp[i - zero][j - one] + 1);
            }
        }
    }
    return dp[m][n];
};

function count (str) {
    let one = 0;
    let zero = 0;
    for (let c of str) {
        if (c === '1') {
            one++;
        } else {
            zero++;
        }
    }
    return [zero, one]; // !!!!!! It is an array, not object, you must change here if you want to change order, not just the place of destructruing!!!! Stupid.
}
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 09:38:15 | 只看该作者
全局:
Make up
0815 1. Plus One

/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
    digits.reverse();
    // !!!! the most important one, add the 1
    let carry = 1;
    for (let i = 0; i < digits.length; i++) {
        const val = carry + digits[i];
        digits[i] = val % 10;
        carry = Math.floor(val / 10);
    }
    if (carry > 0) {
        digits.push(1);
    }
    return digits.reverse();
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 16:40:33 | 只看该作者
全局:
Make up

0815 2. Increasing Subsequences

/**
* @param {number[]} nums
* @return {number[][]}
*/
var findSubsequences = function(nums) {
    if (nums == null || nums.length < 2) {
        return [];
    }
    let res = [[]];
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        const start = map.has(num) ? map.get(num) : 0;
        const len = res.length;
        map.set(num, len);
        for (let i = start; i < len; i++) {
            const list = res[i];
            if (list.length === 0 || list[list.length - 1] <= num) {  // !!!! the empty case
                const listCopy = list.slice();
                listCopy.push(num);
                res.push(listCopy);
            }
        }
    }
    return res.filter(list => list.length > 1);
};

This last one is exhausting. So hard to get right.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-15 16:42:45 | 只看该作者
全局:
Mark. Cleaned up all our debts today. Could have a fresh start from tomorrow.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-16 11:53:06 | 只看该作者
全局:
1015 1. Search a 2D Matrix II

/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
    if (matrix == null || matrix.length === 0) {
        return false;
    }
    const rowLen = matrix.length;
    const colLen = matrix[0].length;
    let i = rowLen - 1;
    let j = 0;
    while (i >= 0 && j < colLen) {
        const num = matrix[i][j];
        if (num === target) {
            return true;
        } else if (target < num) { // !!! which one goes first !!!
            i--;
        } else {
            j++;
        }
    }
    return false;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-16 11:59:04 | 只看该作者
全局:
1015 2. Power of Four

/**
* @param {number} num
* @return {boolean}
*/
var isPowerOfFour = function(num) {
    if (num <= 0) {return false;}
    while (num !== 1) {
        if (num % 4 !== 0) {return false;}
        num = num >>> 2;
    }
    return true;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-10-16 12:01:10 | 只看该作者
全局:
回复

使用道具 举报

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

本版积分规则

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