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

记录

🔗
 楼主| sicilianee 2017-8-17 15:03:37 | 只看该作者
全局:
0816 Wednesday

2. Two Sum II - Input array is sorted

/**
* @param {number[]} numbers
* @param {number} target
* @return {number[]}
*/
var twoSum = function(numbers, target) {
    const dict = {};
    for (let i = 0; i < numbers.length; i++) {
        const number = numbers;
        const complement = target - number;
        if (dict[complement] != undefined) {
            return [dict[complement], i + 1];
        } else {
            dict[number] = i + 1;
        }
    }
    throw new Error('Not found');
};


TODO:
1. Two pointers could do without extra space

回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-18 11:58:35 | 只看该作者
全局:
8017 Thursday

1. Best Time to Buy and Sell Stock II

/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
    let memo = 0;
    for (let i = 1; i < prices.length; i++) {
        const curr = prices[i];
        const prev = prices[i - 1];
        if (curr > prev) {
            memo += curr - prev;
        }
    }
    return memo;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-18 13:11:20 | 只看该作者
全局:
0817

2. Next Greater Element II

/**
* @param {number[]} nums
* @return {number[]}
*/
var nextGreaterElements = function(nums) {
    const doubleNums = [...nums, ...nums];
    const result = [];
    const stack = [];
    let i = 0;
    while (i < doubleNums.length) {
        if (stack.length === 0) {
            stack.push(i);
            i++;
            continue;
        }
        const top = doubleNums[stack[stack.length - 1]];
        const value = doubleNums;
        if (value > top) {
            const index = stack.pop();
            result[index] = value;
        } else {
            stack.push(i);
            i++;
        }
    }
    while (stack.length > 0) {
        const index = stack.pop();
        result[index] = -1;
    }
    return result.slice(0, doubleNums.length / 2);
};

TODOS:
Combine the continue into the else branch. Otherwise it is likely that you miss the 'continue' keyword.


回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-18 14:03:45 | 只看该作者
全局:
本帖最后由 sicilianee 于 2017-8-18 14:05 编辑

0818 Friday

1. Minimum Index Sum of Two Lists

/**
* @param {string[]} list1
* @param {string[]} list2
* @return {string[]}
*/
var findRestaurant = function(list1, list2) {
    // first put list1 into dict
    // then, loop list 2 and collect min while you loop
    const dict = {};
    list1.forEach((value, index) => dict[value] = index);
    let min = Number.MAX_VALUE;
    let results = [];
    list2.forEach((value, index) => {
        if (dict[value] != undefined) {
            const sum = index + dict[value];
            if (sum === min) {
                results.push(value);
            } else if (sum < min) {
                results = [value];
                min = sum; // !!! you still need to update it!!!
            }
        }
    });
    return results;
};


要注意更新最小值
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-18 14:19:47 | 只看该作者
全局:
0818 Friday
2. Sum of Left Leaves

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumOfLeftLeaves = function(root) {
    if (root == null) {
        return 0;
    }
    return getSum(root, false);
   
   
    function getSum (node, left) {
        // stop:
        if (node == null) {
            throw new Error('Illegal Argument');
        }
        if (node.left == null && node.right == null) {
            return left ? node.val : 0;
        }
        const leftSum = node.left != null ? getSum(node.left, true) : 0;
        const rightSum = node.right != null ? getSum(node.right, false) : 0;
        return leftSum + rightSum;
    }
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-18 14:40:44 | 只看该作者
全局:
本帖最后由 sicilianee 于 2017-8-18 14:44 编辑

For last Tuesday:
1. Add One Row to Tree


/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} v
* @param {number} d
* @return {TreeNode}
*/
var addOneRow = function(root, v, d) {
    if (d === 1) {
        const newRoot = new TreeNode(v);
        newRoot.left = root;
        return newRoot;
    }
    const target = d - 1;
    doModify(root, 1);
    return root;
   
    function doModify (node, depth) {
        if (node == null) {return;}
        if (depth === target) {
            const left = node.left;
            const right = node.right;
            node.left = new TreeNode(v);
            node.left.left = left;
            node.right = new TreeNode(v);
            node.right.right = right;
        } else {
            doModify(node.left, depth + 1);
            doModify(node.right, depth + 1);
        }
    }
};
1. You could actually use the original signature to do recursion, but this one is easier for me to understand.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-21 07:33:36 | 只看该作者
全局:
0819 Saturday

1. Linked List Random Node

/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param head The linked list's head.
        Note that the head is guaranteed to be not null, so it contains at least one node.
* @param {ListNode} head
*/
var Solution = function(head) {
    this.head = head;
};

/**
* Returns a random node's value.
* @return {number}
*/
Solution.prototype.getRandom = function() {
    let poolNode = this.head;
    let node = poolNode;
    let index = 0;
    while (node !== null) {
        const randomIndex = Math.floor(Math.random() * (index + 1));        
        if (randomIndex === 0) {
            poolNode = node;  
        }
        // update
        node = node.next;
        index++;
    }
    return poolNode.val;
};

/**
* Your Solution object will be instantiated and called as such:
* var obj = Object.create(Solution).createNew(head)
* var param_1 = obj.getRandom()
*/
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-21 08:03:25 | 只看该作者
全局:
0819

2. Binary Tree Tilt

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var findTilt = function(root) {
    buildSum(root);
    let tiltSum = 0;
    getTilt(root);
    return tiltSum;
   
    function getTilt (node) {
        if (node == null) {
            return;
        }
        const left = node.left != null ? node.left.sum : 0;
        const right = node.right != null ? node.right.sum : 0;
        tiltSum += Math.abs(left - right);
        getTilt(node.left);
        getTilt(node.right);
    }
   
    function buildSum (node) {
        if (node == null) {
            return 0;
        }
        if (node.sum != undefined) {
            return node.sum;
        }
        const left = buildSum(node.left);
        const right = buildSum(node.right);
        node.sum = node.val + left + right;
        return node.sum;
    }
};

回复

使用道具 举报

🔗
 楼主| sicilianee 2017-8-21 15:51:11 | 只看该作者
全局:
0820 Sunday

1. Total Hamming Distance

class Solution {
    public int totalHammingDistance(int[] nums) {
        int size = nums.length;
        int count = 0;
        for (int i = 0; i < 32; i++) {
            int numOfOnes = 0;
            int mask = 1 << i;
            for (int num : nums) {
                int increment = (num & mask) == 0 ? 0 : 1;
                numOfOnes += increment;
            }
            count += numOfOnes * (size - numOfOnes);
        }
        return count;
    }
}

Sadly this problem can only be solved by Java because js doens't have integers like Java or c++.
回复

使用道具 举报

🔗
susu_susu 2017-8-22 00:10:44 | 只看该作者
本楼:
全局:
楼主加油
回复

使用道具 举报

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

本版积分规则

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