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 <= 100
3 <= logs[i].length <= 100
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:
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.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...
随机推荐
- BMP格式转JPEG格式
int Bmp2Jpg(const char *bmp_data, const char *jeg_file, const int width, const int height) { int ret ...
- Spring单例Bean和线程安全
Spring的bean默认都是单例的,这些单例Bean在多线程程序下如何保证线程安全呢?例如对于Web应用来说,Web容器对于每个用户请求都创建一个单独的Sevlet线程来处理请求,引入Spring框 ...
- 基于OpenCV的火焰检测(一)——图像预处理
博主最近在做一个基于OpenCV的火焰检测的项目,不仅可以检测图片中的火焰,还可以检测视频中的火焰,最后在视频检测的基础上推广到摄像头实时检测.在做这个项目的时候,博主参考了很多相关的文献,用了很多种 ...
- O2O和B2C、C2C的区别
B2C.C2C是在线支付,购买的商品会塞到箱子里通过物流公司送到你手中;O2O是在线支付,购买线下的商品.服务,再到线下去享受服务. O2O模式的核心很简单,就是把线上的消费者带到现实的商店中去.在线 ...
- windows黑科技-记录dns log
昨天看到袁哥微博,看到了这篇,今天测试了一下,记录下来: The DNS Client service does not log by default. However, if a file name ...
- Android CTS
1.什么是CTS CTS是google制定的兼容性测试包(Compatibility Test Suite),只有通过CTS测试的设备才有可能获得Android的商标和享受Android Market ...
- 安卓SQLite数据库操作(上)
安卓系统自带数据库,名为SQLite.这篇文章我们用一个Demo来讲解安卓操作数据库的例子. By the way, 安卓创建的数据库文件存放在/data/data/<包名>/databa ...
- 人工智能二之Sublime Text3环境配置
1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...
- apache2不识别php
sudo apt-get install libapache2-mod-php7.0 sudo a2enmod php7.0 sudo service apache2 restart 注意:Apach ...
- ascii#ascii,对象类中找__repr__,获取其返回值
#!/usr/bin/env python #ascii,对象类中找__repr__,获取其返回值 class Foo : def __repr__(self): return "hello ...