Leetcode-937-Reorder Log Files-(Easy)
一、题目描述
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:
0 <= logs.length <= 1003 <= logs[i].length <= 100logs[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)的更多相关文章
- 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 ...
- 【Leetcode_easy】937. Reorder Log Files
problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- 【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 ...
- 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 ...
- 【LeetCode】Reorder Log Files(重新排列日志文件)
这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...
- leecode 937 Reorder Log Files (模拟)
传送门:点我 You have an array of logs. Each log is a space delimited string of words. For each log, the ...
- [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 ...
- LeetCode.937-重新排序日志数组(Reorder Log Files)
这是悦乐书的第358次更新,第385篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第220题(顺位题号是937).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...
- [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 ...
随机推荐
- Spring Cloud Config:外部集中化配置管理
Spring Cloud Config:外部集中化配置管理 SpringCloud学习教程 SpringCloud Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置 ...
- redis 简单整理——复制的原理[二十三]
前言 简单介绍一下复制的原理. 正文 在从节点执行slaveof命令后,复制过程便开始运作,下面详细介绍建立 复制的完整流程. 1)保存主节点(master)信息. 执行slaveof后从节点只保存主 ...
- SQL Server实战一:创建、分离、附加、删除、备份数据库
本文介绍基于Microsoft SQL Server软件,实现数据库创建.分离.附加.删除与备份的方法. 目录 1 交互式创建数据库 2 Transact-SQL指定参数创建数据库 3 交互式分离 ...
- HarmonyOS NEXT 实战开发—Grid和List内拖拽交换子组件位置
介绍 本示例分别通过onItemDrop()和onDrop()回调,实现子组件在Grid和List中的子组件位置交换. 效果图预览 使用说明: 拖拽Grid中子组件,到目标Grid子组件位置,进行两者 ...
- 阿里开源自研工业级稀疏模型高性能训练框架 PAI-HybridBackend
简介:近年来,随着稀疏模型对算力日益增长的需求, CPU集群必须不断扩大集群规模来满足训练的时效需求,这同时也带来了不断上升的资源成本以及实验的调试成本.为了解决这一问题,阿里云机器学习PAI平台开 ...
- AHPA:开启 Kubernetes 弹性预测之门
简介:阿里巴巴云原生团队和阿里达摩院决策智能时序团队合作开发 AHPA 弹性预测产品,该产品主要出发点是基于检测到的周期做"定时规划",通过规划实现提前扩容的目的,在保证业务稳定 ...
- 如何在零停机的情况下迁移 Kubernetes 集群
简介:本文将通过集群迁移的需求.场景以及实践方式,介绍如何基于阿里云容器服务 ACK,在零停机的情况下迁移 Kubernetes 集群. 作者:顾静(子白)|阿里云高级研发工程师:谢瑶瑶(初扬)|阿 ...
- Serverless 场景排查问题利器 : 函数实例命令行操作
简介:实例命令行功能的推出希望能消除用户使用 Serverless 的"最后一公里",直接将真实的函数运行环境展现给用户. 背景介绍 全托管的 Serverless 计算平台能给 ...
- [FE] FastAdmin 动态下拉组件 Selectpage 自定义 data-params
正常情况下,我们想获取列表只需要定义接口路径和要显示的字段名即可, 比如: <input id="c-package_ids" data-rule="require ...
- 实验8 #第8章 Verilog有限状态机设计-3 #Verilog #Quartus #modelsim
3. 状态机A/D采样控制电路 3.1 目标:用状态机控制ADC0809实现数据采集. 3.2 ADC0809简介 (1)ADC0809是8位A/D转换器,片内有8路模拟开关,可控制8个 模拟量中 的 ...