资讯专栏INFORMATION COLUMN

[LeetCode] 937. Reorder Log Files

nemo / 1665人阅读

Problem

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:

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

Solution
class Solution {
    public String[] reorderLogFiles(String[] logs) {
        Comparator comparator = new Comparator() {
            @Override
            public int compare(String s1, String s2) {
                int i1 = s1.indexOf(" ");
                int i2 = s2.indexOf(" ");
                char c1 = s1.charAt(i1+1);
                char c2 = s2.charAt(i2+1);
                if (c1 <= "9") {
                    return c2 <= "9" ? 0 : 1;
                } else if (c2 <= "9") {
                    return -1;
                } else {
                    //all letters
                    int res = s1.substring(i1+1).compareTo(s2.substring(i2+1));
                    if (res == 0) return s1.substring(0,i1).compareTo(s2.substring(0,i2));
                    else return res;
                }
            }
        };
        
        Arrays.sort(logs, comparator);
        return logs;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72989.html

相关文章

  • Leetcode PHP题解--D54 937. Reorder Log Files

    摘要:题目链接题目分析给定一个数组,每一个元素是一条日志。剩余部分为全为小写字母的字符串称为字符日志或全为数字的字符串称为数字日志。给定的数组中确定会至少有一个字母。遍历完成后,对字符日志进行排序。在其后拼接数字日志数组,并返回即可。 D54 937. Reorder Log Files 题目链接 937. Reorder Log Files 题目分析 给定一个数组,每一个元素是一条日志。 ...

    hot_pot_Leo 评论0 收藏0
  • [LeetCode] 143. Reorder List

    Problem Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not modify the values in the lists nodes, only nodes itself may be changed. Example 1: Given 1->2->...

    miracledan 评论0 收藏0
  • [Leetcode] Reorder List 链表重新排序

    摘要:要找到后半部分的起点,就是用快慢指针。不过该题我们不能直接拿到中间,而是要拿到中间的前一个节点,这样才能把第一个子链表的末尾置为空,这里的技巧就是快慢指针循环的条件是。注意因为不能有额外空间,我们最好用迭代的方法反转链表。 Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→...

    hufeng 评论0 收藏0
  • [LeetCode] 280. Wiggle Sort

    Problem Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] nums[i-1]) swap(nums, i, i-1); } } } private void swap(int[] nums, int i, int j) { ...

    archieyang 评论0 收藏0
  • [LeetCode] 556. Next Greater Element III

    Problem Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive ...

    _ang 评论0 收藏0

发表评论

0条评论

nemo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<