937. 重新排列日志文件( 简单自定义排序 )

Question

937. 重新排列日志文件

在这里插入图片描述

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Ideas

1、Answer( Java )

解法思路:简单自定义排序

Code

/**
 * @author Listen 1024
 * @description 937. 重新排列日志文件 (简单模拟排序题)
 * 时间复杂度 O(nlogn)
 * 空间复杂度 O(n)
 * @date 2022-05-03 12:04
 */
class Solution {
    public String[] reorderLogFiles(String[] logs) {
        String[] res = new String[logs.length];
        ArrayList<String> listNumDig = new ArrayList<>();
        ArrayList<String> listStrDig = new ArrayList<>();
        for (String log : logs) {
            String[] strings = log.split("\\s+");
            int i = strings[1].charAt(0);
            if (i >= 48 && i <= 57) {//judge the num from [0,9] of the ASCII
                listNumDig.add(log);
            } else {
                listStrDig.add(log);
            }
        }
        listStrDig.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int index1 = o1.indexOf(" ");
                int index2 = o2.indexOf(" ");
                String temp1 = o1.substring(index1, o1.length());
                String temp2 = o2.substring(index2, o2.length());
                if (!temp1.equals(temp2)) {
                    return temp1.compareTo(temp2);
                } else {
                    return o1.substring(0, index1).compareTo(o2.substring(0, index2));
                }
            }
        });
        int index = 0;
        for (int i = 0; i < listStrDig.size(); i++) {
            res[i] = listStrDig.get(i);
            index++;
        }
        for (int i = 0; i < listNumDig.size(); i++) {
            res[index] = listNumDig.get(i);
            index++;
        }
        return res;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值