leetcode:渲染日志文件,无法排序

我试图将以下c ++答案(来自上面的讨论)转换为javascript。


static bool myCompare(string a, string b){

        int i = a.find(' ');

        int j = b.find(' ');

        if(isdigit(a[i + 1]))

            if(isdigit(b[j + 1]))

                return false;       // a b are both digit logs, a == b, keep their original order

            else

                return false;       // a is digit log, b is letter log, a > b

        else

            if(isdigit(b[j + 1]))

                return true;        // a is letter log, b is digit log, a < b

            else {

                if (a.substr(i) == b.substr(j))

                    return a.substr(0,i) < b.substr(0,j); //If string part is the same, compare key

                else

                    return a.substr(i) < b.substr(j);   // a and b are both letter

            }

    }


    vector<string> reorderLogFiles(vector<string>& logs) {

        //The order of equal elements is guaranteed to be preserved in stable_sort.

        //Use sort() cannot pass the OJ. 

        stable_sort(logs.begin(), logs.end(), myCompare);

        return logs;

    }

我的解决方案如下:


var reorderLogFiles = function(logs) {

    return logs.sort(cmp);

}


var cmp = function(a, b) {

    let i = a.indexOf(' ');

    let j = b.indexOf(' ');


    if(isdigit(a[i + 1])) {

        if(isdigit(b[j + 1])) {

            // a, b digit, a == b

            return 0;

        } else {

            // a digit, b letter, b|a

            return 1;

        }

    } else {

        let condi;


        // a letter, b digit, a|b

        if(isdigit(b[j + 1])) {

            return -1;

        } else {

            // both letter

            if (a.substring(i+1) === b.substring(j+1)) {

                // start from space, all same, compare key

                condi = a.substring(0,i).localeCompare(b.substring(0,j));                

                //console.log('same', condi, a.substring(0,i), b.substring(0,j));


                return condi;

            } else {    

             }

    }


}


我的输出与预期输出之间的差异:


"s 1088746413789"


米琪卡哇伊
浏览 123回答 1
1回答

萧十郎

您无法在javascript中仅通过在比较其他字符串时返回0而不是来实现稳定排序。您将不得不将其索引存储在地图中,然后做出相应的判断。我已经修改了您的代码,如下所示:0var map = {};var reorderLogFiles = function(logs) {&nbsp; &nbsp; logs.forEach(function(value,index){&nbsp; &nbsp; &nbsp; &nbsp; map[value] = index;&nbsp; &nbsp; });&nbsp; &nbsp; return logs.sort(cmp);}var cmp = function(a, b) {&nbsp; &nbsp; let i = a.trim().indexOf(' ');&nbsp; &nbsp; let j = b.trim().indexOf(' ');&nbsp; &nbsp; if(isDigit(a[i+1])){&nbsp; &nbsp; &nbsp; &nbsp; if(isDigit(b[j+1])) return map[a] - map[b];&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp; &nbsp; if(isDigit(b[j+1])){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; let cond = a.substring(i+1).localeCompare(b.substring(j+1));&nbsp; &nbsp; if(cond == 0) return a.substring(0,i).localeCompare(b.substring(0,j));&nbsp; &nbsp; return cond;}var isDigit = function(letter) {&nbsp; &nbsp; return !isNaN(letter);}首先,我们使用它的出现索引创建一个字符串映射。以后,我们用来确定a和和何时b是数字日志。如果两者都是字母日志,则比较忽略标识符的日志。发生冲突时使用标识符。您的代码存在如下问题:// both letter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a.substring(i+1) === b.substring(j+1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // start from space, all same, compare key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; condi = a.substring(0,i).localeCompare(b.substring(0,j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log('same', condi, a.substring(0,i), b.substring(0,j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return condi;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; condi = a.substring(i+1).localeCompare(b.substring(j+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log('not same', condi, a.substring(i+1), ' | ', b.substring(j+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return condi;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }在这里,您将比较字母日志而不考虑标识符。但是,假设两者相同(在并列情况下),您无需检查其标识符的字典顺序。此外,首字母字符匹配在通常情况下也不起作用。解决此问题的最佳方法是将字母日志和数字日志收集在不同的数组中,然后仅对字母日志进行排序,最后附加数字日志。如果许多日志是经过大量测试的数字日志,则这将为您提供更好的平均案例性能。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript