传送门:点我

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:

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. 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;
}
};

最新文章

  1. Windows 10 RTM 官方正式版
  2. PYTHON第三天
  3. Cheatsheet: 2015 01.01~ 01.31
  4. js parseInt();parseFloat;Number()
  5. Android--ViewPager的无限轮播
  6. C# richTextBox编辑器
  7. [转]Android访问网络,使用HttpURLConnection还是HttpClient
  8. Attribute在.NET编程中的应用(三)
  9. 基于Spring Aop实现类似shiro的简单权限校验功能
  10. lambda表达式初步
  11. C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法
  12. 来聊一聊不low的Linux命令——find、grep、awk、sed
  13. 微信小程序 table 简单测试
  14. 10 Tips for Writing Better Code (阅读理解)
  15. javadate相关
  16. CSS text-decoration 属性
  17. [BZOJ3173]最长上升子序列
  18. odoo 开发基础 -- postgresql重新启动、状态查看
  19. EFCore中SQLSERVER 2008 的分页问题
  20. Xamarin iOS教程之使用按钮接接收用户输入

热门文章

  1. feign client 的简单使用(1)
  2. HTML5 图片宽高自适应,居中裁剪不失真
  3. vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i
  4. day43-socketserver
  5. eclipse,import,导入项目显示红色叹号
  6. 应用SharedPreference保存程序的配置信息
  7. Servlet基本_セッション属性
  8. 尚硅谷springboot学习4-helloworld探究
  9. js相关文章
  10. AWK 知识库