博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leecode 937 Reorder Log Files (模拟)
阅读量:5354 次
发布时间:2019-06-15

本文共 2361 字,大约阅读时间需要 7 分钟。

传送门:

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 = 0;        while(ss1>>s){            if(k > 0){                news1 = news1 + s +" ";             }            k++;        }        k = 0;        stringstream ss2(s2);        while(ss2>>s){            if(k > 0){                news2 = news2 + s +" ";             }            k++;        }        if(news1
reorderLogFiles(vector
& logs) { vector
p1,p2,ans; for(int i = 0 ; i < logs.size() ; i++){ string s = logs[i]; if(s[s.size()-1]<='9' && s[s.size()-1] >= '0'){ p2.push_back(s); } else{ p1.push_back(s); } } sort(p1.begin(),p1.end(),cmp); for(int i = 0 ; i < p1.size() ; i ++){ ans.push_back(p1[i]); } for(int i = 0 ; i < p2.size() ; i ++){ ans.push_back(p2[i]); } return ans; }};

 

转载于:https://www.cnblogs.com/Esquecer/p/10325217.html

你可能感兴趣的文章
Zookeeper 学习(二) zookeeper的安装
查看>>
从网页上抓取图片
查看>>
Pro Git - 笔记1
查看>>
知乎布局||offsetTop||侧边栏自动等高
查看>>
JAVA运算符
查看>>
日期和时间总结
查看>>
用原生VideoView进行全屏播放时的问题
查看>>
JS Window对象
查看>>
bootstrap学习笔记
查看>>
IPython: 超越普通Python(1)
查看>>
CSS3-Canvas画布(渐变)
查看>>
python 练完这些,你的函数编程就ok了
查看>>
codeforces 300E Empire Strikes Back 数论+二分查找
查看>>
文件夹分类操作
查看>>
对Slony-I中wait on的理解
查看>>
Codeforces 899B Months and Years
查看>>
hibernate4整合spring3事务问题
查看>>
Ink – 帮助你快速创建响应式邮件(Email)的框架
查看>>
CutJS – 用于 HTML5 游戏开发的 2D 渲染引擎
查看>>
OS.js – 开源的 Web OS 系统,赶快来体验
查看>>