传送门:点我

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;
}
};

leecode 937 Reorder Log Files (模拟)的更多相关文章

  1. 【Leetcode_easy】937. Reorder Log Files

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

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

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

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

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

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

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

  7. 【LeetCode】Reorder Log Files(重新排列日志文件)

    这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...

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

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

  9. URAL 2073 Log Files (模拟)

    题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...

随机推荐

  1. [Python]实践:实现探测Web服务质量

    来源:Python 自动化运维 技术与最佳实践 HTTP服务是最流行的互联网应用之一,服务质量的好坏关系到用户体验以及网站的运营服务水平,最常用的有两个标准:1.服务的可用性,比是否处于正常提供服务状 ...

  2. GridView Bind 短日期 格式

    ASP.NET的GridView控件的编辑模板中,需要绑定数据库中的某个字段,如<%# Bind("startTime","{0:d}") %> 在 ...

  3. as2 连接服务器 post

    import mx.utils.Delegate; //接收服务器数据的文本加载器 var result_lv:LoadVars; /** * 数据提交成功后 * 获取的数据 * @param suc ...

  4. eclipse,import,导入项目显示红色叹号

    [场景]eclipse导入项目后,该项目出现红色叹号.打开类文件,会有无数的“The type ... cannot be resolved.”等稀奇古怪的编译错误. [分析]当前eclipse环境中 ...

  5. js判断网页是否加载完毕

    1. document.onreadystatechange = function () { if(document.readyState=="complete") { docum ...

  6. python实现最大重叠子串的查找

    #!/usr/bin/python #查找最大重叠子串 def FindMaxDup(in_str): str_len = len(in_str) result = '' #逐级扩大搜索长度# lev ...

  7. webpack打包avalon+oniui+jquery

    随着avalon的发展壮大,我根据CSDN的统计数字,中国前端大概有1%的人在使用avalon了. avalon的最大优势是能兼容IE6,并且其API是非常稳定,只是在1.3.7 对ms-duplex ...

  8. windows下配置mysql环境变量 - 使用cmd访问mysql(图)

    window7为例,右击“计算机” - 单击“属性” - 单击“高级系统设置” - 单击“环境变量”,剩下看图: <图1> 右下角"环境变量". <图2>选 ...

  9. Java中关键字static的使用与作用

    1.static的意义 static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和 ...

  10. 18.异常.md

    目录 1.try...catch 2.异常了的继承机制 2.1基本概念 2.2常用异常 2.3多异常捕获 2.4获取异常信息 2.5finally回收资源 2.6Checked异常和Runtime异常 ...