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 <= 1003 <= logs[i].length <= 100logs[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, ...
随机推荐
- centos7.5下kubeadm安装kubernetes集群安装
文章是按https://blog.csdn.net/Excairun/article/details/88962769,来进行操作并记录相关结果 版本:k8s V14.0,docker-ce 18.0 ...
- hadoop集群namenode同时挂datanode
背景:(测试环境)只有两台机器一台namenode一台namenode,但集群只有一个结点感觉不出来效果,在namenode上挂一个datanode就有两个节点,弊端见最后 操作非常简单(添加独立节点 ...
- CustomJSProperties珍藏版。目的是减少客户端的代码数量,但是又能将服务器数据传输给客户端。关键是:数据是实时更新的!!!!
[这是帮助文档的说明] CustomJSProperties EventThe CustomJSProperties event fires each time a control callback ...
- delphi 属性 参数 新注释
delphi 属性 参数 新注释,在写代码的时候,可以自动看到属性.参数的的备注说明,太方便了. Tmyclass=class /// <summary> /// 姓名 /// </ ...
- js乱码问题解决
乱码有可能出现在下面两种情况 1.高级浏览器直接访问js路径 2.jsp引用js 针对上述两种情况的解决方式: 1.查看设置浏览器的字符集 2.查看web服务器的字符集,比如Tomcat 配置UTF- ...
- js 正则函数初级
1.test :正则匹配字符串,如果成功则返回true,若果失败则返回false 格式:/正则表达式/.test(字符串) 默认吗匹配规则,区分大小写:如果不区分大小写,则加修饰符 i 例子: < ...
- Bash 翻译
Bash参考手册 目录 1简介 1.1什么是Bash? 1.2什么是shell? 2定义 3基本外壳功能 3.1 Shell语法 3.1.1外壳操作 3.1.2报价 3.1.2.1逃逸角色 3.1.2 ...
- unity "[ ]"标签
[CanEditMultipleObjects]//可多对象编辑 public class Collider2DEditor:Editor {} [SerializeField]//序列化私有属性 p ...
- 如何用jar命令生成可执行的jar文件
如果你已经把zh.java文件生成了zh.class文件 如果zh.class的路径是bin/com/zhang/zh.class 则在bin目录下放一个manifest.mf文件,文件内容如下: M ...
- PHP搜索中的sql注入
----------------------------------------------------------------------------------------- 防止查询的sql攻击 ...