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

记录

🔗
 楼主| sicilianee 2017-11-25 07:56:07 | 只看该作者
全局:
1124 10.  Strobogrammatic Number

/**
* @param {string} num
* @return {boolean}
*/
var isStrobogrammatic = function(num) {
    if (num == null || num.length === 0) {
        return false;
    }
    // !!! order will switch, so 0, 1, 8 will also need to be checked
    let left = 0;
    let right = num.length - 1;
    while (left <= right) {
        const leftChar = num[left];
        const rightChar = num[right];
        if (['1', '8', '0'].includes(leftChar)) {
            if (rightChar !== leftChar) {
                return false;
            }
        } else if (leftChar === '6') {
            if (rightChar !== '9') {
                return false;
            }
        } else if (leftChar === '9') {
            if (rightChar !== '6') {
                return false;
            }
        } else {
            return false;
        }
        left++;
        right--;
    }
    return true;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 08:05:29 | 只看该作者
全局:
1124 11. Valid Word Square

/**
* @param {string[]} words
* @return {boolean}
*/
var validWordSquare = function(words) {
    if (words == null || words.length === 0) {
        return false;
    }
    for (let i = 0; i < words.length; i++) {
        for (let j = 0; j < words[i].length; j++) {
            if (!words[j]) { // !!! need to guard it, one has it one doesn't is false
                return false;
            }
            if (words[i][j] !== words[j][i]) { // !!!! i, j mixed !!!
                return false;
            }
        }
    }
    return true;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 09:11:52 | 只看该作者
全局:
1124 12. Paint Fence

/**
* @param {number} n
* @param {number} k
* @return {number}
*/
var numWays = function(n, k) {
    if (n === 0) {return 0;}
    if (n === 1) {return k;}
    let same = k;
    let diff = k * (k - 1);
    for (let i = 2; i < n; i++) {
        // could use ;[] = []; to do the assignment.
        // use same color as prev one
        const useSame = diff;
        // use diff color as prev one
        const useDiff = (k - 1) * (same + diff);
        same = useSame;
        diff = useDiff;
    }
    return same + diff;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 09:24:48 | 只看该作者
全局:
1124 13. Maximum Distance in Arrays

/**
* @param {number[][]} arrays
* @return {number}
*/
var maxDistance = function(arrays) {
    if (arrays == null || arrays.length === 0) {
        return 0;
    }
    let minVal = arrays[0][0];
    let maxVal = arrays[0][arrays[0].length - 1];
    let max = 0;
    for (let i = 1; i < arrays.length; i++) {
        const array = arrays[i];
        const currMax = array[array.length - 1];
        const currMin = array[0];
        max = Math.max(max, Math.abs(currMax - minVal), Math.abs(maxVal - currMin));
        maxVal = Math.max(maxVal, currMax);
        minVal = Math.min(minVal, currMin);
    }
    return max;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 10:23:05 | 只看该作者
全局:
1124 14. Max Stack

/**
* initialize your data structure here.
*/
var MaxStack = function() {
    this.stack = [];
    this.maxStack = [];
};

/**
* @param {number} x
* @return {void}
*/
MaxStack.prototype.push = function(x) {
    this.stack.push(x);
    if (this.maxStack.length === 0) {
        this.maxStack.push(x);
    } else {
        const max = Math.max(x, this.maxStack[this.maxStack.length - 1]);
        this.maxStack.push(max);
    }
};

/**
* @return {number}
*/
MaxStack.prototype.pop = function() {
    this.maxStack.pop();
    return this.stack.pop();
};

/**
* @return {number}
*/
MaxStack.prototype.top = function() {
    return this.stack[this.stack.length - 1];
};

/**
* @return {number}
*/
MaxStack.prototype.peekMax = function() {
    return this.maxStack[this.maxStack.length - 1];
};

/**
* @return {number}
*/
MaxStack.prototype.popMax = function() {
    const tmpStack = [];
    while (this.stack[this.stack.length - 1] !== this.maxStack[this.maxStack.length - 1]) {
        tmpStack.push(this.stack.pop());
        this.maxStack.pop();
    }
    const num = this.stack.pop();
    this.maxStack.pop();
    while (tmpStack.length > 0) {
        this.push(tmpStack.pop());
    }
    return num;
};

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

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 12:37:08 | 只看该作者
全局:
1124 15. Design Compressed String Iterator

/**
* @param {string} compressedString
*/
var StringIterator = function(compressedString) {
    compressedString = compressedString || '';
    let i = 0;
    const list = [];
    while (i < compressedString.length) {
        const c = compressedString[i];
        let j = i + 1;
        while (j < compressedString.length && compressedString[j] >= '0' && compressedString[j] <= '9') {
            j++;
        }
        const str = compressedString.slice(i + 1, j);
        const num = Number.parseInt(str, 10);
        list.push([c, num]);
        i = j;
    }
    this.list = list;
    this.i = 0;
    this.j = 0;
};

/**
* @return {character}
*/
StringIterator.prototype.next = function() {
    if (!this.hasNext()) {return ' '} // !!! retunr a white space, not empty string
    const [c, num] = this.list[this.i];
    this.j++;
    if (this.j === num) {
        this.i++;
        this.j = 0;
    }
    return c;
};

/**
* @return {boolean}
*/
StringIterator.prototype.hasNext = function() {
    return this.i < this.list.length;
};

/**
* Your StringIterator object will be instantiated and called as such:
* var obj = Object.create(StringIterator).createNew(compressedString)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 13:14:53 | 只看该作者
全局:
1124 16. Read N Characters Given Read4

This made it clear:
I agree with you regarding the ambiguity of the problem, especially for non-C++ programmers. It should explicitly say read4 reads 4 characters from a file into "buf" char array, and implement a function that reads n characters from the file into "buf" char array.

/**
* Definition for read4()
*
* @param {character[]} buf Destination buffer
* @return {number} The number of characters read
* read4 = function(buf) {
*     ...
* };
*/

/**
* @param {function} read4()
* @return {function}
*/
var solution = function(read4) {
    /**
     * @param {character[]} buf Destination buffer
     * @param {number} n Maximum number of characters to read
     * @return {number} The number of characters read
     */
    return function(buf, n) {
        if (n <= 0) {return 0;}
        let i = 0;
        while (i + 4 <= n) {
            const tmp = [];
            const num = read4(tmp);
            buf.push(...tmp)
            if (num !== 4) {
                return i + num;
            }
            i = i + 4;
        }
        const remain = n - i;
        const tmp = [];
        const num = read4(tmp);
        const minNum = Math.min(remain, num);
        for (let j = 0; j < minNum; j++) {
            buf.push(tmp[j]);
        }
        return minNum + i;
    };
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 13:17:19 | 只看该作者
全局:
sicilianee 发表于 2017-11-25 13:14
1124 16. Read N Characters Given Read4

This made it clear:

could combine everything into the loop, i.e. put the last iteration into the loop.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 13:47:04 | 只看该作者
全局:
1124 17. Valid Word Abbreviation

/**
* @param {string} word
* @param {string} abbr
* @return {boolean}
*/
var validWordAbbreviation = function(word, abbr) {
    const list = [];
    let i = 0;
    while (i < abbr.length) {
        if (abbr[i] >= '0' && abbr[i] <= '9') {
            let j = i;
            while (j < abbr.length && abbr[j] >= '0' && abbr[j] <= '9') {
                j++;
            }
            const str = abbr.slice(i, j);
            if (str[0] === '0') {return false} // !!! cannot have leading 0s, even a single 0 is not allowed !!!!
            const num = Number.parseInt(str, 10);
            list.push(num);
            i = j; // !!!! this update !!!!
        } else {
            list.push(abbr[i]);
            i++;
        }
    }
    let j = 0;
    for (let el of list) {
        if (Number.isInteger(el)) {
            j = j + el;
        } else {
            if (j >= word.length || word[j] !== el) {
                return false;
            }
            j++;
        }
    }
    return j === word.length;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-11-25 14:09:19 | 只看该作者
全局:
1124 18. Two Sum III - Data structure design

/**
* Initialize your data structure here.
*/
var TwoSum = function() {
    this.map = new Map();
};

/**
* Add the number to an internal data structure..
* @param {number} number
* @return {void}
*/
TwoSum.prototype.add = function(number) {
    const count = this.map.get(number) || 0;
    this.map.set(number, count + 1);
};

/**
* Find if there exists any pair of numbers which sum is equal to the value.
* @param {number} value
* @return {boolean}
*/
TwoSum.prototype.find = function(value) {
    console.log(this.map)
    for (let [number, count] of this.map.entries()) {
        const target = value - number;
        if (this.map.has(target)) {
            if (target !== number || this.map.get(number) > 1) { // !!!! if it is false, you cannot return early because
                // you still have other entries to be tested
                return true;
            }
        }
    }
    return false;
};

/**
* Your TwoSum object will be instantiated and called as such:
* var obj = Object.create(TwoSum).createNew()
* obj.add(number)
* var param_2 = obj.find(value)
*/
回复

使用道具 举报

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

本版积分规则

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