这道题是LeetCode里的第937道题。

题目描述:

你有一个日志数组 logs。每条日志都是以空格分隔的字串。

对于每条日志,其第一个字为字母数字标识符。然后,要么:

  • 标识符后面的每个字将仅由小写字母组成,或;
  • 标识符后面的每个字将仅由数字组成。

我们将这两种日志分别称为字母日志和数字日志。保证每个日志在其标识符后面至少有一个字。

将日志重新排序,使得所有字母日志都排在数字日志之前。字母日志按字母顺序排序,忽略标识符,标识符仅用于表示关系。数字日志应该按原来的顺序排列。

返回日志的最终顺序。

示例 :

输入:["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
输出:["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

提示:

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. logs[i] 保证有一个标识符,并且标识符后面有一个字。

题目的意思很简单,但是由于处理的是字符串,处理起来就会相对的麻烦一些。

首先我们先要用一个 List 结构来保存日志的文章开头下标,即除去标识符和一个空格,例如:日志 "a1 9 2 3 1" 我们保存它的下标为3,其它的日志也是相同的道理。保存后在转成一个 Object[] 数组,因为对 List 不是很熟练而且 List 不支持下标访问,所以使用了这个方法来简化后面的操作。然后就是排序了,我这里使用的排序方法类似于插入排序:就是抽取一本日志,然后在把这本日志合适的插入已经排好序的日志堆里。排序遍历的时候需要注意的几个问题:

  • 拿到的日志是那种日志
  • 日志插在哪个位置才合适
  • 要如何处理逻辑关系

解题代码:

class Solution {
public static String[] reorderLogFiles(String[] logs){
List<Integer>recodeIndex = new ArrayList<>(); for(String log : logs) {
for(int i = 0;i < log.length(); i++) {
if(log.charAt(i) == ' ') {
recodeIndex.add(i + 1);
break;
}
}
} Object[] recode = recodeIndex.toArray(); for(int i = 0; i < logs.length; i++) {//遍历logs
if(logs[i].charAt((int) recode[i])>='0'&&logs[i].charAt((int) recode[i])<='9')continue;
int insert = i;
int numCount = 0;
for(int j = 0; j < i; j++) {//遍历已排序好的logs
if(logs[j].charAt((int) recode[j])>='0'&&logs[j].charAt((int) recode[j])<='9')
{numCount++;continue;}//判断是否为数字日志 int len = logs[j].length();//另一本日志的长度
int sortIndex = (int) recode[j];//另一本日志的开头位置
for(int k = (int) recode[i]; k < logs[i].length(); k++) {//寻找插入位
if(len < k || logs[i].charAt(k) > logs[j].charAt(sortIndex)){break;}
else if(logs[i].charAt(k) == logs[j].charAt(sortIndex)) {sortIndex++;continue;}
else {insert = j;break;}
}
if(insert < i)break;
} int tempIndex = recodeIndex.get(i);
String temp = new String(logs[i]);
for(int l = i;l > insert - numCount; l--) {
logs[l] = logs[l - 1];
recode[l] = recode[l - 1];
}
logs[insert - numCount] = temp;//同时不要忘记交换对应的日志开头下标数组
recode[insert - numCount] = tempIndex;
} return logs;
}
}

解题结果:

过程感悟:

我在编写算法的过程中一部分时间浪费在忘记处理 recode 数组上了,调试后才发现问题,在一个就是交换的 for 循环的循环次数和上下界,当时由于代码的逻辑混乱造成了一些失误,现在的代码是我简化后的结果,当然可以再次简化。或许这道题可以使用哈希表或者队列来解决。

附记:lambda表达式

【LeetCode】Reorder Log Files(重新排列日志文件)的更多相关文章

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

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

  2. 【Leetcode_easy】937. Reorder Log Files

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

  3. Linux /var/log下的各种日志文件详解

    1)/var/log/secure:记录登录系统存取数据的文件;例如:pop3,ssh,telnet,ftp等都会记录在此. 2)/var/log/wtmp:记录登录这的信息记录,被编码过,所以必须以 ...

  4. java最简单实现Log打印和生成日志文件

    导包 1.commons-logging.jar包 下载 2.log4j.jar包 下载 配置log4j 1.在src根目录下创建一个log4j.properties文件. 文件全部内容如下: log ...

  5. [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 ...

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

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

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

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

  8. 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 ...

  9. 【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 ...

随机推荐

  1. java错误笔记

    错误1. Description       Resource Path Location   Type The superclass "javax.servlet.http.HttpSer ...

  2. MySQL检查死锁简介

  3. ajax传给springMVC数据编码集问题

    前台 ajax: $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串. { dataType : &q ...

  4. (转)SpringMVC学习(二)——SpringMVC架构及组件

    http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...

  5. 改变console.log的输出样式

    console.log允许你通过css来格式化输出,格式如下: console.log(‘%c字符串%c字符串’, 样式1, [样式2]) 其中”%c”为模板字符串 例子: 1 console.log ...

  6. django连接Oracle过程中出现的问题

    开始时版本信息: python 3.6   +   ce_oracle 6 最终版本信息: python 3.5   + ce_oracle 5.2 ce_oracle版本问题 cx_Oracle-5 ...

  7. Robot Framework(十一) 执行测试用例——后处理输出

    3.3后处理输出 在测试执行期间生成的XML输出文件可以在之后由rebot工具进行后处理,该工具是Robot Framework的组成部分.在测试执行期间生成测试报告和日志时会自动使用它,但在执行后也 ...

  8. python之dic {字典}(重要指数*****)

    1. 什么是字典 {'name': '汪峰', 'age': 18} '键':'值' 别的语言键值对数据 键: 必须是可哈希(不可变的数据类型),并且是唯一的 值: 任意 可以保存任意类型的数据 字典 ...

  9. Oracle 回顾

    Oracle 函数 日期函数: 1.sysdate--查询当前日期 select sysdate from dual;  --查询当前日期 2.months_between--返回两个日期之间的月份差 ...

  10. JsonUtils工具类

    public class JsonUtils { public static void printTimeObject(Object obj, HttpServletResponse response ...