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 ...
随机推荐
- 《c#高级编程》第2章C#2.0中的更改(二)——匿名类型
一.概念 C#中的匿名类型是一种特殊类型,可以在运行时动态创建一个对象,该对象可以包含多个属性,这些属性的名称和类型可以在创建时指定.相对于定义具体的类,匿名类型更加灵活和简洁. C#的匿名类型通常用 ...
- 直播回顾:准确性提升到 5 秒级,ssar 独创的 load5s 指标有多硬核?| 龙蜥技术
简介: 你还在为分析机器负载高而苦恼?这款 ssar 工具独创 load5s 指标精准定位超硬核. 编者按:本文整理自龙蜥SIG技术周会,作者闻茂泉,阿里云计算平台事业部SRE运维专家,是龙蜥社区跟 ...
- Log4j漏洞不仅仅是修复,更需要构建有效预警机制
简介:软件的漏洞有时不可避免,根据Gartner的相关统计,到 2025 年,30% 的关键信息基础设施组织将遇到安全漏洞.日志服务SLS,可帮助快速部署一个预警机制,使得漏洞被利用时可以快速发现并 ...
- [Go] go build 和 go install 的区别
$ go build 源文件及其包依赖 编译成二进制. install 不仅执行build过程 而且会把编译的二进制放到 $GOPATH/bin/,包放到 $GOPATH/pkg/ Link:http ...
- [Go] go-nsq 使用指南
首先你需要有一个 nsq 的服务端,nsq 由三部分构成:nsqd.nsqlookupd.nsqadmin. 快速启动 nsq 一个节点看这里:https://github.com/farwish/n ...
- WPF 将控件放入到 UserControl 里获取 HwndSource 为空的情况
本文记录将 WPF 控件放入到 UserControl 里,如果此 UserControl 没有被设置 Visibility 为可见过,那么放在此 UserControl 内的控件将获取不到 Hwnd ...
- dotnet C# 如果在构造函数抛出异常 析构函数是否会执行
假设在某个类型的构造函数里面抛出了异常,那么这个对象的析构函数是否会执行 如下面代码 private void F1() { try { _ = new Foo(); } catch { // 忽略 ...
- 阿里面试Redis最常问的三个问题:缓存雪崩、击穿、穿透(带答案)
那提到Redis我相信各位在面试,或者实际开发过程中对缓存雪崩,穿透,击穿也不陌生吧,就算没遇到过但是你肯定听过,那三者到底有什么区别,我们又应该怎么去防止这样的情况发生呢,我们有请下一位受害者. 面 ...
- 6.prometheus监控--监控redis/rabbitmq/mongodb
1.监控redis 1.1 redis_exporter安装方式 1.1.1 二进制源码安装方式 参考nginx二进制安装方法 redis_exporter下载地址:https://github.co ...
- VSCode+VUE+ESLint以达到保存自动格式化
首先打开VSCode在.eslintrc.js中加入以下代码(不知道怎么找可以ctrl+shift+p进行搜索),添加 vscode 终端启动服务 // 添加⾃定义规则 'prettier/prett ...