leecode 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.
大意:给你一些日志,包括英文日志和数字日志,每个日志又包括日志头和日志内容,日志头是第一个单词,日志内容全数字的是数字日志,全英文的是英文日志。要求排序后输出。
排序规则:对英文的日志,去掉日志头,按日志内容字典序排序。对数字的日志,按输入顺序。
总体上,英文日志在前,数字日志在后。
思路:对每个字符串的最后一位进行判断之后,分类到两个向量里,对英语日志用stringstream进行分割,然后sort排序。
代码:
class Solution {
public:
static bool cmp(string s1,string s2){
string news1="",news2="";
stringstream ss1(s1);
string s;
int k = ;
while(ss1>>s){
if(k > ){
news1 = news1 + s +" ";
}
k++;
}
k = ;
stringstream ss2(s2);
while(ss2>>s){
if(k > ){
news2 = news2 + s +" ";
}
k++;
}
if(news1<news2) return true;
return false;
//return news1 < news2;
}
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string>p1,p2,ans;
for(int i = ; i < logs.size() ; i++){
string s = logs[i];
if(s[s.size()-]<='' && s[s.size()-] >= ''){
p2.push_back(s);
}
else{
p1.push_back(s);
}
}
sort(p1.begin(),p1.end(),cmp);
for(int i = ; i < p1.size() ; i ++){
ans.push_back(p1[i]);
}
for(int i = ; i < p2.size() ; i ++){
ans.push_back(p2[i]);
}
return ans;
}
};
leecode 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 ...
- 937. 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】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).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...
- URAL 2073 Log Files (模拟)
题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...
随机推荐
- Spring Cloud (5)hystrix 服务监控
1.pom 2.启动类 3. 微服务提供方 pom 4. 监控------已成功启动 --------------------------------------------------------- ...
- leetcode 题解 word search。递归可以更简洁。
先写上我的代码: 我总是不知道何时把任务交给下一个递归.以致于,写出的代码很臃肿! 放上别人递归的简洁代码: bool exist(char** board, int m, int n, char* ...
- redis 学习笔记1(安装以及控制台命令)
为什么要学习这个? 分布式技术必会,得益于redis的设计理念,内存数据库,epoll(多路复用)模型,单线程模型除去了锁和上下文切换,提高了性能.单线程保证执行顺序(轮询),在分布式环境下对于数据的 ...
- DataGrip 连接数据库查询出来的结果乱码的问题
打开连接数据源选项 选择 Advanced----Charset 填入 GBK 应用即可 目前遇到的是连接 SYbase数据库
- 尚硅谷springboot学习15-日志框架1-入门
引子 小张:开发一个大型系统 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息:日志框架 : ...
- python语言中的数据类型之元组
数据类型 元组 tuple 元组:不可变类型 用途:元组就是一个不可变的列表,当需要存不改动的值时可用元组 定义方式:在()内用逗号分隔开多个任意类型的元素 t=(1,2.2,'aa',( ...
- Zookeeper 在Linux系统的安装
注册中心Zookeeper 官方推荐使用 zookeeper 注册中心.注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小. Z ...
- How to Pronounce Work vs. Walk
How to Pronounce Work vs. Walk Share Tweet Share Tagged With: Comparison If you’re confused about th ...
- debian下redis2.8.17安装过程
下载redis源码包,我下载的是redis2.8.17 解压缩该源码包 tar zxf redis-2.8.17.tar.gz 进入解压缩后的目录 cd redis-2.8.17/ 添加redis用户 ...
- 通过使用浏览器对象模型,输出当前浏览器窗口中打开的文档的URL信息,并将显示在窗口中。
<script type="text/javascript">window.document.write("这个网页文件来自:".bold());w ...