[LeetCode] 937. Reorder Data in Log Files

#937. Reorder Data in Log Files

문제

입력 받은 로그들을 문자로 이루어진 로그는 문자 알파벳순으로, 숫자 로그는 입력 순서로 정렬한다. 같은 문자의 문자로그는 식별자에 따라 정렬한다.

반환 순서는 정렬된 문자로그 + 입력 순서대로의 숫자로그 이다.

설명

우선 제가 생각한 방법은 입력받은로그를 문자로그, 숫자로그로 분류하고, 숫자로그는 입력순으로 저장한다.

문자로그는 앞에 식별자를 제외하고, 정렬작업을 진행한다.

그 후 정렬된 문자로그 + 숫자로그의 배열을 반환한다.

사용된 메소드 설명 split() join() shift() push() sort()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var reorderLogFiles = function(logs) {
  const digit = []; // 숫자로그를 저장할 배열
  const letter = []; // 문자로그를 저장할 배열
  const result = []; // 최종으로 반환할 배열
  for (let log of logs) {
    const arr = log.split(' ');// 입력 받은 로그를 split()으로 나눠서
    arr.shift(); // 첫 번째는 식별자 이므로 제외하고.
    if (!isNaN(Number(arr.join('')))) { // 로그를 배열로 쪼갠걸 합쳐서 숫자로그인지 문자로그인지 확인
      digit.push(log);
    } else {
      letter.push(log);
    }
  }

  letter.sort((a, b) => { // 문자로그는 정렬해야되고, 문자가 같다면
    let aStr = a.split(' '); // 로그를 쪼개서 배열로 만들고
    const aIdentifier = aStr.shift(); // 같은 문자일 결우 사용할 식별자를 추출하고
    aStr = aStr.join(' '); // 배열을 문자로 만든다.
    let bStr = b.split(' ');
    const bIdentifier = bStr.shift();
    bStr = bStr.join(' ');
    if(aStr > bStr) return 1;
    if(aStr < bStr) return -1;
    if(aStr === bStr) { //문자가 같다면 식별자로 순서를 정한다.
      if (aIdentifier > bIdentifier) return 1;
      if (aIdentifier < bIdentifier) return -1;
    }
    return 0;
  });
  for (let i = 0; i < letter.length; i++) { // 정렬된 문자로그를 반환할 배열에 먼저 넣고
    result.push(letter[i])
  }
  for (let i = 0; i < digit.length; i++) { // 입력 순서대로인 숫자 로그릴 반환할 배열에 넣는다.
    result.push(digit[i])
  }
  return result;
};

LeetCode

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

  • Letter-logs: All words (except the identifier) consist of lowercase English letters.
  • Digit-logs: All words (except the identifier) consist of digits.

Reorder these logs so that:

  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering. Return the final order of the logs.

Example 1:

1
2
3
4
5
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"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

Example 2:

1
2
Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

Constraints:

1 <= logs.length <= 100 3 <= logs[i].length <= 100 All the tokens of logs[i] are separated by a single space. logs[i] is guaranteed to have an identifier and at least one word after the identifier.

Built with Hugo
Theme Stack designed by Jimmy