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;
}
};
最新文章
- Windows 10 RTM 官方正式版
- PYTHON第三天
- Cheatsheet: 2015 01.01~ 01.31
- js parseInt();parseFloat;Number()
- Android--ViewPager的无限轮播
- C# richTextBox编辑器
- [转]Android访问网络,使用HttpURLConnection还是HttpClient
- Attribute在.NET编程中的应用(三)
- 基于Spring Aop实现类似shiro的简单权限校验功能
- lambda表达式初步
- C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法
- 来聊一聊不low的Linux命令——find、grep、awk、sed
- 微信小程序 table 简单测试
- 10 Tips for Writing Better Code (阅读理解)
- javadate相关
- CSS text-decoration 属性
- [BZOJ3173]最长上升子序列
- odoo 开发基础 -- postgresql重新启动、状态查看
- EFCore中SQLSERVER 2008 的分页问题
- Xamarin iOS教程之使用按钮接接收用户输入
热门文章
- feign client 的简单使用(1)
- HTML5 图片宽高自适应,居中裁剪不失真
- vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i
- day43-socketserver
- eclipse,import,导入项目显示红色叹号
- 应用SharedPreference保存程序的配置信息
- Servlet基本_セッション属性
- 尚硅谷springboot学习4-helloworld探究
- js相关文章
- AWK 知识库