Leetcode
2020.04.25 12:17

937. Reorder Data in Log Files

조회 수 165 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

You have an array of logs.  Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumeric identifier.  Then, either:

  • Each word after the identifier will consist only of lowercase letters, or;
  • Each word after the identifier will consist only of digits.

We will call these two varieties of logs letter-logs and digit-logs.  It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log.  The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties.  The digit-logs should be put in their original order.

Return the final order of the logs.

 

Example 1:

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"]

 

Constraints:

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. logs[i] is guaranteed to have an identifier, and a word after the identifier.


class Solution {
    public String[] reorderLogFiles(String[] logs) {
        Arrays.sort(logs, (str1, str2)->{
            String[] sp1 = str1.split(" ", 2);
            String[] sp2 = str2.split(" ", 2);
            
            boolean digit1 = Character.isDigit(sp1[1].charAt(0));
            boolean digit2 = Character.isDigit(sp2[1].charAt(0));
            
            if(digit1 && digit2){
                return 0;
            }else if(digit1 || digit2){
                return digit1 ? 1 : -1;
            }else{
                int compare = sp1[1].compareTo(sp2[1]);
                if(compare == 0){
                    return sp1[0].compareTo(sp2[0]);
                }
                return compare;
            }
            /*
            if(!digit1 && !digit2){
                if(sp1[1].compareTo(sp2[1]) == 0){
                    return sp1[0].compareTo(sp2[0]);
                }
                return sp1[1].compareTo(sp2[1]);
            }
            return digit1 ? (digit2 ? 0 : 1) : -1;
            */
        });
        
        return logs;
    }
}


[문제] https://leetcode.com/problems/reorder-data-in-log-files/



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
77 Leetcode [todo] 687. Longest Univalue Path hooni 2020.05.01 211
76 Programmers team game secret hooni 2020.04.30 0
75 Programmers hashmap secret hooni 2020.04.30 0
74 Programmers fib secret hooni 2020.04.30 0
73 Leetcode 997. Find the Town Judge hooni 2020.05.02 718
72 Leetcode 994. Rotting Oranges file hooni 2020.04.14 177
71 Leetcode 993. Cousins in Binary Tree file hooni 2020.04.30 222
70 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 172
69 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 162
» Leetcode 937. Reorder Data in Log Files hooni 2020.04.25 165
67 Leetcode 897. Increasing Order Search Tree hooni 2020.05.04 687
66 Leetcode 876. Middle of the Linked List hooni 2020.05.04 678
65 Leetcode 872. Leaf-Similar Trees file hooni 2020.05.04 679
64 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 165
63 Leetcode 844. Backspace String Compare hooni 2020.05.05 748
62 Leetcode 783. Minimum Distance Between BST Nodes hooni 2020.05.01 728
61 Leetcode 763. Partition Labels hooni 2020.04.17 222
60 Leetcode 75. Sort Colors hooni 2020.04.14 155
59 Leetcode 731. My Calendar II hooni 2020.04.15 159
58 Leetcode 729. My Calendar I hooni 2020.04.15 151
Board Pagination Prev 1 2 3 4 Next
/ 4