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.

Approach #1: Sort. [Java]

class Solution {
public String[] reorderLogFiles(String[] logs) {
Comparator<String> myComp = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int s1si = s1.indexOf(' ');
int s2si = s2.indexOf(' ');
char s1fc = s1.charAt(s1si+1);
char s2fc = s2.charAt(s2si+1);
if (s1fc <= '9') {
if (s2fc <= '9') return 0;
else return 1;
}
if (s2fc <= '9') return -1;
int preCompute = s1.substring(s1si+1).compareTo(s2.substring(s2si+1));
if (preCompute == 0)
return s1.substring(0, s1si).compareTo(s2.substring(0, s2si));
return preCompute;
}
}; Arrays.sort(logs, myComp);
return logs;
}
}

  

Analysis:

This solution takes advantage of what we're guaranteed in the problem:

1. guaranteed to have a word following an identifier (allows me to use indexOf(' ‘) freely.).

2.letter logs need to be ordered lexicographically, so we can use built in compare function when we know we have two.

3. number logs need to be sorted naturally, so we just say they are all "equal" to each other and trust java's built in sort feature to be stable.

4. number logs need to be after letter logs, so once we find out they're differenct, we return one of the other and short-circuit.

Reference:

https://leetcode.com/problems/reorder-log-files/discuss/193872/Java-Nothing-Fancy-15-lines-5ms-all-clear.

http://www.cnblogs.com/skywang12345/p/3324788.html

937. Reorder Log Files的更多相关文章

  1. 【Leetcode_easy】937. Reorder Log Files

    problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...

  2. LeetCode 937 Reorder Log Files 解题报告

    题目要求 You have an array of logs.  Each log is a space delimited string of words. For each log, the fi ...

  3. leecode 937 Reorder Log Files (模拟)

    传送门:点我 You have an array of logs.  Each log is a space delimited string of words. For each log, the ...

  4. 【leetcode】937. Reorder Log Files

    题目如下: You have an array of logs.  Each log is a space delimited string of words. For each log, the f ...

  5. 【LeetCode】937. Reorder Log Files 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...

  6. [Swift]LeetCode937. 重新排列日志文件 | Reorder Log Files

    You have an array of logs.  Each log is a space delimited string of words. For each log, the first w ...

  7. 【LeetCode】Reorder Log Files(重新排列日志文件)

    这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...

  8. LeetCode.937-重新排序日志数组(Reorder Log Files)

    这是悦乐书的第358次更新,第385篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第220题(顺位题号是937).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...

  9. Leetcode937. Reorder Log Files重新排列日志文件

    你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...

随机推荐

  1. ASP.NET MVC5中View显示Html

    @Html.Raw(Model.Name) @(new HtmlString(Model.Name));

  2. 新建一个Model类的注意事项

    昨天在工作中新建了一个Model类在测试环境测试一点问题也没有,到了生产环境就报错了,由于调用的是分页类,报错说:在520行 _count() 函数不存在. 我的思路是:先到生产环境查看了具体的报错文 ...

  3. [转载]Ubuntu下ssh服务的安装与登陆(ssh远程登陆)

    转载地址:http://blog.csdn.net/zht666/article/details/9340633 Ubuntu默认并没有安装ssh服务,如果通过ssh远程连接到Ubuntu,需要自己手 ...

  4. [转]ubuntu11.04配置nfs--解决mount.nfs: access denied问题

    总算通过了nfs的localhost测试. 配置很简单,下面摘自网络,并且整理下: 1 安装nfs #apt-get install nfs-kernel-server #apt-get instal ...

  5. java代码----方法类练习中~~~未领会

    总结:今天我很失败,问了老师继承的问题还是没弄懂.因为,技术宅的能力,我好怕啊.太强了. package com.da.ima2; public class yut {// 使用substring方法 ...

  6. Windows下.svn文件夹的最简易删除方法(附linux)

    如果想删除Windows下的.svn文件夹,通过手动删除的渠道是最麻烦的,因为每个文件夹下面都存在这样的文件.下面是一个好办法:建立一个文本文件,取名为kill-svn-folders.reg(扩展名 ...

  7. JVM Class Loading过程

    转自:<Java Performance>第三章 VM Class Loading The Hotspot VM supports class loading as defined by ...

  8. Ubuntu安装Chrome及hosts修改

    Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo  ...

  9. 4-4 zk特性 – 理解watcher机制

    watcher是zk里面非常重要的特性.watcher一定要去好好地看一下,一定要去好好地理解一下它是如何去用的,包括触发的事件类型等等.监督者也可以理解为触发器,也就是说当我们的节点发生了一些变化的 ...

  10. [codevs1159]最大全0子矩阵(悬线法)

    解题关键:悬线法模板题.注意此模板用到了滚动数组. #include<cstdio> #include<cstring> #include<algorithm> # ...