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:
0 <= logs.length <= 1003 <= logs[i].length <= 100logs[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:
937. Reorder Log Files的更多相关文章
- 【Leetcode_easy】937. Reorder Log Files
problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...
- 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 ...
- leecode 937 Reorder Log Files (模拟)
传送门:点我 You have an array of logs. Each log is a space delimited string of words. For each log, the ...
- 【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 ...
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- [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 ...
- 【LeetCode】Reorder Log Files(重新排列日志文件)
这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...
- LeetCode.937-重新排序日志数组(Reorder Log Files)
这是悦乐书的第358次更新,第385篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第220题(顺位题号是937).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...
- Leetcode937. Reorder Log Files重新排列日志文件
你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...
随机推荐
- MySQL 5.6 date 与 string 的转换和比较
我们有张表,表中有一个字段 dpt_date ,SQL 类型为 date,表示离开日期. 我们将 dpt_date 与字符串 ‘2016-03-09’ 进行比较,发现效率低于 dpt_date 转换为 ...
- Celery-4.1 用户指南: Canvas: Designing Work-flows(设计工作流程)
签名 2.0 版本新特性. 刚刚在calling 这一节中学习了使用 delay 方法调用任务,并且通常这就是你所需要的,但是有时候你可能想将一个任务调用的签名传递给另外一个进程或者作为另外一个函数的 ...
- python IOError: windows directory not found at xxxxx win32
您需要修改 PATH 环境变量,将Python的可执行程序及额外的脚本添加到系统路径中.将以下路径添加到 PATH 中: C:\Python2.7\;C:\Python2.7\Scripts\;请打开 ...
- [MySQL]修改mysql数据库的root密码的方法
方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass ...
- 2015.3.7 Dll CString不能作为传入参数而要用char*
extern "C" __declspec(dllexport) void CalcArc_2(Point2D& pm, double am, double an, CSt ...
- Recovery of DISKGROUP in VXVM (ZT)
http://gurkulindia.com/main/2012/03/recovery-of-diskgroup-in-vxvm-veritas-volume-manager/# Since lon ...
- springmvc 在页面跳转之后 引入文件的路径前面加上了 controller 的映射名
转自:https://zhidao.baidu.com/question/2140453086362943788.html 应该是没有前面的/user的 前端用的是jsp吗,如果是在路径前加${pag ...
- 监控和安全运维 1.5 nagios监控客户端-1
3. Nagios安装 - 客户端(192.168.0.12)在客户端机器上 rpm -ivh http://www.aminglinux.com/bbs/data/attachment/forum/ ...
- C# WinForm中如何让当前应用程序只允许启动一个实例
我们在WinForm开发中,很多情况下是需要只允许让用户运行一个实例,那么代码其实很简单.只需要修改Program.cs文件,代码如下 static class Program { /// <s ...
- [Python Study Notes]物体运动检测
基于opencv的cv2模块实现 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...