leetcode讲解--937. Reorder Log Files

题目

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

Note:

  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.

题目地址

讲解

这道题我看过后立马联想到了另一个题目:953. Verifying an Alien Dictionary。那个题目是判断是否有序,而现在这个题目是要排序。感觉这一题更难。

实际做下来确实还是比较难的,主要是字符串的比较,要自己写比较规则。然后在调用系统的sort函数。如果要自己写快排那又要多花不少时间了。

有个小窍门,题目要求数字的logs保持原样,而且要放在数组尾部,所以我这里是从后往前遍历数组。

Java代码

class Solution {
    public String[] reorderLogFiles(String[] logs) {
        String[] result = new String[logs.length];
        int indexBegin = 0;
        int indexEnd = logs.length-1;
        for(int i=logs.length-1;i>=0;i--){
            int firstSpaceIndex = logs[i].indexOf(' ');
            if(logs[i].charAt(firstSpaceIndex+1)>='0' && logs[i].charAt(firstSpaceIndex+1)<='9'){
                result[indexEnd--] = logs[i];
            }else{
                result[indexBegin++] = logs[i];
            }
        }
        // for(int i=0;i{
        @Override
        public int compare(String o1, String o2) {
            int o1IndexOfFirstSpace = o1.indexOf(' ');
            o1 = o1.substring(o1IndexOfFirstSpace);
            int o2IndexOfFirstSpace = o2.indexOf(' ');
            o2 = o2.substring(o2IndexOfFirstSpace);
            int minLength = o1.length();
            boolean o1ShortThano2 = true;
            if(o1.length()>o2.length()){
                minLength = o2.length();
                o1ShortThano2 = false;
            }
            boolean o1LittleThano2 = true;
            boolean o1Equalso2 = true;
            for(int i=0;io2.charAt(i)){
                    o1LittleThano2 = false;
                    o1Equalso2 = false;
                    break;
                }
            }
            if(o1Equalso2){
                if(o1ShortThano2){
                    return -1;
                }else{
                    return 1;
                }
            }else{
                if(o1LittleThano2){
                    return -1;
                }else{
                    return 1;
                }
            }
        }
    }
}

你可能感兴趣的:(数组,排序,字符串,算法,leetcode)