一、题目描述

  

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.

  解释:

  给定一个字符串的数组,每个字符串中间用空格分割,第一个元素表示索引,后面的为内容;内容分为两种,一种是纯字符串,一种是纯数组

  要求进行排序,排序规则是,纯字符串内容的排在纯数字的前面;对于纯字符串的元素,比较索引大小排序;对于纯数字的其相对位置不变

  

二、解答:

class Solution {
public:
static bool cmp(const string &A, const string& B){
string subA = A.substr(A.find(' ') + 1);
string subB = B.substr(B.find(' ') + 1);
if(isdigit(subA[0]))
return false;
else if(isdigit(subB[0]))
return true;
return subA.compare(subB) < 0;
} public:
vector<string> reorderLogFiles(vector<string>& logs) { stable_sort(logs.begin(), logs.end(), cmp); return logs;
}
}; int main(int argc, char **argv)
{
Solution *s = new Solution();
cout<<""<<endl; return 0;
}

 

三、学习到的方法

  sort使用的快速排序;stable_sort使用的是merge排序,稳定的,意思是相等的元素前后的位置会保持

  

  另外,string的两个方法

  1、substr(index) ,从0到index的子串

  2、find('') ,表示寻找某个字符所在的位置

Leetcode-937-Reorder Log Files-(Easy)的更多相关文章

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

  2. 【Leetcode_easy】937. Reorder Log Files

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

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

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

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

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

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

  7. leecode 937 Reorder Log Files (模拟)

    传送门:点我 You have an array of logs.  Each log is a space delimited string of words. For each log, the ...

  8. [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

    You have an array of `logs`.  Each log is a space delimited string of words. For each log, the first ...

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

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

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

随机推荐

  1. Maven 三种archetype说明合集【转载】

    Maven 三种archetype说明 新建Maven project项目时,需要选择archetype. 那么,什么是archetype? archetype的意思就是模板原型的意思,原型是一个Ma ...

  2. 重新整理.net core 计1400篇[四] (.net core 修改sdk )

    前言 可能有些人还不知道什么是sdk,software development kit,中文是软件开发包的意思. 然后什么是软件开发包? 软件开发工具包是一些被软件工程师用于为特定的软件包.软件框架. ...

  3. jenkins 持续集成和交付 —— git hook(七)

    前言 这个hook的意思叫做钩子哈,前端听得多. 正文 好吧,这个git hook 有什么用呢? 前面说了一个轮询SCM这个东西呢,我是真的觉得这东西没啥用,经常扫描仓储算怎么回事呢? 但是如果主动通 ...

  4. c# seo 百度sitemap书写

    前言 我们知道对页面百度收录,有两种方式: 1.百度自己抓取. 2.自己提交sitemap让百度来抓取. sitemap 一般的一个业务逻辑是: 生成sitemap xml,然后每隔一段时间更新即可, ...

  5. MMdeploy TensorRT 模型实时监控桌面,PyQt5实现

    本项目遵从:GNU General Public License v3.0 个人博客『 gy77 』: GitHub仓库 :代码源码详见仓库 demo_qt.py 我的CSDN博客 我的博客园 简介: ...

  6. lattice crosslink开发板mipi核心板csi测试dsi屏lif md6000 fpga 常见问题解答

    1. 概述 CrossLink开发板,是用Lattice的芯片CrossLink 家族系列的,LIF-MD6000-6JM80I.该芯片用于桥接视频接口功能,自带2路MIPI硬核的功能,4 LANE  ...

  7. 2.css的导入方式

    三种导入方式: 1.行内样式:在标签元素中,编写一个style属性,编写样式即可.有多个以分号隔开 <h2 style="color: green">标题</h2 ...

  8. 一文搞懂传统单节点网站的 Serverless 上云

    简介: 阿里云函数计算 FC 是事件驱动的全托管计算服务,真正的无需去考虑服务器的运维管理,只需要完成开发的代码进行上传,函数计算会通过角色策略去规划计算资源,弹性的方式执行函数,最后高效的执行部署. ...

  9. 一遇到复杂分析查询就卡顿?MySQL分析实例了解一下

    随着企业数据爆发式增长,MySQL分析查询卡顿问题越来越多,用户时效性不能保证,精细化运营诉求不能满足.如何能无缝对接业务库,实现毫秒级针对万亿级数据进行即时的多维分析透视和业务探索,MySQL分析实 ...

  10. [FAQ] Jetbrains 官网不能访问,获取 Goland 的下载地址

    2020.02 安装包下载 Link:https://www.cnblogs.com/farwish/p/14186441.html