0

所以我试图在stackoverflow上解决这个问题

问题链接:https ://leetcode.com/problems/reorder-data-in-log-files/

问题: 基本上,我们需要重新排序日志,以便所有字母日志都排在任何数字日志之前。字母日志按字典顺序排序,忽略标识符,标识符用于平局。数字日志应按其原始顺序放置。

对于这个问题,我也写了这个

/**
 * @param {string[]} logs
 * @return {string[]}
 */
var reorderLogFiles = function(logs) {
    const letterLogs = []
    const digitLogs = []
    let i = 0; 
    while (i<logs.length) {
        const logger = logs[i]
        const secondWordIndex = logger.indexOf(' ') + 1 
        console.log(logger[secondWordIndex])
        if (isNaN(logger[secondWordIndex])) letterLogs.push(logger)
        else digitLogs.push(logger)
        i++
    }

    return [...letterLogs, ...digitLogs]
};

但这不是按字典顺序排列的,因为我不确定按字典顺序排列是什么意思

这是给定的输入/输出

Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

问题:有人可以帮我为上述问题制定算法吗?我试过做letterLogs.sort((a,b) => a.localeCompare(b)),但也没有用。

4

1 回答 1

1

维基百科:

在数学中,词典或词典顺序(也称为词汇顺序、字典顺序、字母顺序或词典(al)乘积)是单词根据其组成字母的字母顺序按字母顺序排列的方式的概括。这种概括主要包括定义有限全序集合(通常称为字母表)的元素序列(在计算机科学中通常称为字符串)的全序。

字典顺序是在长度比较之前的字母顺序。

也就是说,字符串 a 在字典上小于字符串 b

  • 如果 a 的长度小于 b 的长度,或者
  • 否则它们的长度相同,并且 a 按字母顺序小于 b。

要将项目按顺序排列,必须有一种比较两个项目的方法。对于字符串,通常的顺序基本上是字典顺序,它的字母顺序。

字典顺序与字母顺序

你必须从这个问题中做些什么:

在您的问题中,您应该在dig*.... 字符串之前对所有字符串进行排序,并且在所有字符串中它们应该与字符串相同。let*... let1*lexicographically ordereddig*

检查以下解决方案

请注意,在数组中logs我添加了另一个元素"let0 art can",以便应该发生关系场景。

假设:

  1. 所有字符串都以“let”或“dig”开头。

/*
Basically, we need to Reorder the logs so that all of the letter-logs come before any digit-log. 

The letter-logs are ordered lexicographically ignoring identifiers, 

with the identifier used in case of ties.

The digit-logs should be put in their original order.


*/

let logs = ["dig1 8 1 5 1","let1 art can","let0 art can", "dig2 3 6","let2 own kit dig","let3 art zero"];

//Notice in array logs I have added another element "let0 art can" so that ties scenario should occure.

let letterLogs = logs.filter((log) => log.startsWith("let"));    //get all letter strings 

let digitLogs = logs.filter((log) => log.startsWith("dig"));   //get all digit strings


//custom sort function
let sortFun = function(a, b) {

   let sortResult =  (function removeIdentifiers(a, b){  //remove identifiers
      let aCopy = a.replace(/[0-9]*/gm, "").replace(/\s\s+/gm," ");   //remove digits from the string also replace more than one space with just single space
      let bCopy = b.replace(/[0-9]*/gm, "").replace(/\s\s+/gm," ");;  //remove digits from the string also replace more than one space with just single space
       
      let result = (function sortCopies(a, b){ //sort a and b with identifiers removed
        if(a > b)
          return 1;

        if(b > a)
          return -1;

        return 0;
      })(aCopy, bCopy);

      
      //if identifires removed sort result it 0, i.e. tie the sort with identifiers
      if(result == 0){
         if(a > b)
          return 1;

         if(b > a)
            return -1;

         return 0;
      } else {
        return result;
      }
      
   })(a, b);
   
   return sortResult;  //return the final sort result
   
}

sortedLetterLogs = letterLogs.sort(sortFun)

console.log("sortedLetterLogs", sortedLetterLogs);
/*
sortedLetterLogs [
  "let0 art can",
  "let1 art can",
  "let3 art zero",
  "let2 own kit dig"
]
*/

let finalResult = [...sortedLetterLogs, ...digitLogs ];

console.log("finalResult", finalResult);
/*
finalResult [
  "let0 art can",
  "let1 art can",
  "let3 art zero",
  "let2 own kit dig",
  "dig1 8 1 5 1",
  "dig2 3 6"
]
*/

于 2019-11-06T11:12:20.700 回答