一、题目描述

  

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. 重新点亮shell————测试命令[六]

    前言 我们写好脚本之后希望有一个测试. 正文 介绍一下exit,如果exit 0,那么表示正常退出. 如果exit 10,也就是非0,那么就是异常退出. 然后这个test 标准为[]和 [[]]这样子 ...

  2. JavaSE开发基础--包机制&JavaDoc&Scanner&循环结构&方法&数组

    包机制 如果文件在包中需要 在文件首行添加 package 地址 package pkg1.pkg2.pkg3 import package1 JavaDoc /** * @author作者名 * @ ...

  3. Git 各指令的本质

    前言 作为当前世界上最强大的代码管理工具Git相信大家都很熟悉,但据我所知有很大一批人停留在clone.commit.pull.push...的阶段,是不是对rebase心里没底只敢用merge? 碰 ...

  4. 剑指offer42(Java)-连续子数组的最大和(简单)

    题目: 输入一个整型数组,数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值. 要求时间复杂度为O(n). 示例1: 输入: nums = [-2,1,-3,4,-1,2,1,-5,4 ...

  5. 消息队列Kafka「检索组件」重磅上线!

    ​简介:本文对消息队列 Kafka「检索组件」进行详细介绍,首先通过对消息队列使用过程中的痛点问题进行介绍,然后针对痛点问题提出相应的解决办法,并对关键技术技术进行解读,旨在帮助大家对消息队列 Kaf ...

  6. 深度解析数据湖存储方案Lakehouse架构

    ​简介:从数据仓库.数据湖的优劣势,湖仓一体架构的应用和优势等多方面深度解析Lakehouse架构. 作者:张泊 Databricks 软件工程师 ​ Lakehouse由lake和house两个词组 ...

  7. 阿里云张振尧:阿里云边缘云驱动5G时代行业新价值

    ​简介:近日,以"5G融合通信趋势下的技术创新"为主题的2021中国增值电信及虚拟运营高峰论坛在北京召开,阿里云边缘云高级产品专家张振尧发表了<阿里云边缘云驱动5G时代行业新 ...

  8. [FE] uni-app 动态改变 navigationBarTitleText 导航标题

    改导航文字: uni.setNavigationBarTitle({ title: 'xx' }); 改 tabBar 文字: uni.setTabBarItem({ index: 0, text: ...

  9. WPF 给类库设置设计时使用的资源字典

    在开发 WPF 类库时,由于类库里面没有存在 App.xaml.cs 文件,而在对单个 XAML 进行开发时,设计器将会因为找不到资源文件的存在,而拿不到资源.本文告诉大家简单的方法,给设计器设置仅在 ...

  10. OpenAI未至,Open-Sora再度升级!已支持生成16秒720p视频

    Open-Sora 在开源社区悄悄更新了!现在支持长达 16 秒的视频生成,分辨率最高可达 720p,并且可以处理任何宽高比的文本到图像.文本到视频.图像到视频.视频到视频和无限长视频的生成需求.我们 ...