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.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...
随机推荐
- Windows 7 下将 Tomcat Java 程序设置为 Windows Service
方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...
- Java-API:java.util百科
ylbtech-Java-API:java.util百科 包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机数生成器和位数组.日期 ...
- CSS-MUI:笔记-01
ylbtech-CSS-MUI:笔记 1. mui-navbar 导航条返回顶部 1.1.样式一 1.2. <div class="mui-navbar-inner mui-bar ...
- 发任务找不到test-unit报错
发任务的时候因找不到gem包test-unit报错, 出错行: require 'test/unit' require 'test/unit/testresult' 解决办法如下 1.通过命令查看ge ...
- [更新中]【South使用总结】django开发中使用South进行数据库迁移
Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...
- C语言学习笔记--#error 、 #line 和 #pragma 的使用
1. #error 的用法 (1)#error 是一种预编译器指示字,用于生成一个编译错误消息 (2)用法:#error message //注意:message 不需要用双引号包围 (3)#erro ...
- 【277】◀▶ Python 列表/元组/字典说明
目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...
- 【265】shell文件创建链接
优点:可以在其他文件夹内运行对应的*.sh文件,例如通过putty会默认进入的文件夹,可以将链接文件放在那里,就可以直接调用了! 方法:很简单 1. 在文件上点击右键>创建链接 2. 可以对下面 ...
- 记录一次从txt文件导入数据的python下的MySQL实现
环境: python2.7 ComsenzXP自带MySQL 安装python-MySQL模块 数据格式:txt格式的账号信息. 数据一行一条数据. 难点:有的行只有账号,没有密码:有的为空行:有的行 ...
- saltstact的安装与配置
Saltstack是一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,人们一般习惯把saltstack比作成简化版的puppet和加强版的func.saltstack基于Pytho ...