Skip to content

Conversation

@Qiu-IT
Copy link
Member

@Qiu-IT Qiu-IT commented Jun 9, 2023

第1940题JS做法 谢谢

@yanglbme
Copy link
Member

yanglbme commented Jun 9, 2023

@Qiu-IT

一些声明可以换成 const,map 的遍历,可以简化一下:

/**
 * @param {number[][]} arrays
 * @return {number[]}
 */
var longestCommonSubsequence = function (arrays) {
    const m = new Map();
    const rs = [];
    const len = arrays.length;
    for (let i = 0; i < len; i++) {
        for (let j = 0; j < arrays[i].length; j++) {
            m.set(arrays[i][j], (m.get(arrays[i][j]) || 0) + 1);
        }
    }
    for (const [k, v] of m) {
        if (v === len) {
            rs.push(k);
        }
    }
    return rs;
};

@Qiu-IT
Copy link
Member Author

Qiu-IT commented Jun 9, 2023

谢谢提醒 请问下面这个写法 算可以简化了麽

/**
 * @param {number[][]} arrays
 * @return {number[]}
 */
var longestCommonSubsequence = function(arrays) {
    const m = new Map();
    const rs = [];
    const len = arrays.length;
    for (let i=0;i<len;i++) {
        for (let j=0;j<arrays[i].length;j++) {
            m.set(arrays[i][j],(m.get(arrays[i][j]) || 0)+1);
            if (m.get(arrays[i][j])===len) rs.push(arrays[i][j]);
        }
    }
    return rs;
};

@Qiu-IT
Copy link
Member Author

Qiu-IT commented Jun 9, 2023

我去提交一份 用您修改好的 谢谢

@yanglbme
Copy link
Member

yanglbme commented Jun 9, 2023

可以,格式化一下 @Qiu-IT

@yanglbme yanglbme merged commit b8630f5 into doocs:main Jun 9, 2023
@Qiu-IT Qiu-IT deleted the s1940 branch June 10, 2023 09:16
@Qiu-IT Qiu-IT changed the title feat: add js solution to lc problem:No.1940 feat: add js solution to lc problem: No.1940 Jun 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants