public class Solution
{
public string[] ReorderLogFiles(string[] logs)
{
var list1 = new List<string>();
var list2 = new List<string>();
foreach (var log in logs)
{
var spacePosition = log.IndexOf(' ');
var c = log[spacePosition + ];
if (c >= && c <= )
{
list2.Add(log);
}
else
{
list1.Add(log);
}
}
var list = list1.OrderBy(x => x.Substring(x.IndexOf(' ') + )).ToList();
list.AddRange(list2);
return list.ToArray();
}
}

补充一份C++的实现:

bool cmp(const pair<string, string>& a, const pair<string, string>& b) {
return a.second < b.second;
}
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
map<string, string> V1;//记录文本log
vector<string> V2;//记录数字log
for (auto log : logs) {
int spacePosition = ;
for (int i = ; i < log.length(); i++) {
if (log[i] == ' ') {
spacePosition = i;//找到第一个空格的下标
break;
}
}
char c = log[spacePosition + ];//找到空格后第一个字符
if (c >= && c <= ) {//ASCII码在48~57,表示数字0~9
V2.push_back(log);//存储数字log
}
else {
//Key值是原始log,Value值是去除头部的identifier之后的内容
V1.insert(make_pair(log, log.substr(spacePosition + )));
}
}
//将map转换为vector
vector<pair<string, string>> vec(V1.begin(), V1.end()); //按照value值排序
sort(vec.begin(), vec.end(), cmp); vector<string> V;//记录最终结果
for (auto p : vec) {
V.push_back(p.first);
}
for (auto p : V2) {
V.push_back(p);
}
return V;
}
}; int main()
{
Solution S;
vector<string> V;
V.push_back("a1 9 2 3 1");
V.push_back("g1 act car");
V.push_back("zo4 4 7");
V.push_back("ab1 off key dog");
V.push_back("a8 act zoo");
vector<string> V2 = S.reorderLogFiles(V);
for (auto v : V2) {
cout << v << endl;
}
}

提交显示错误的问题,我这里没有遇到,截图如下:

leetcode937的更多相关文章

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

  2. Leetcode937. Reorder Log Files重新排列日志文件

    你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...

随机推荐

  1. pipelinedb 滑动窗口

    滑动窗口可以方便的让我们进行一段时间的数据分析 几个主要函数 clock_timestamp 内置的函数,总是返回当前的时间戳 arrival_timestamp 事件达到的时间 单滑动窗口 参考 C ...

  2. Swift 学习笔记十五:扩展

    扩展就是向一个已有的类.结构体或枚举类型加入新功能(functionality).扩展和 Objective-C 中的分类(categories)相似.(只是与Objective-C不同的是,Swif ...

  3. 使用MVC5的Entity Framework 6入门 ---- 系列教程

    使用MVC5的Entity Framework 6入门(十二)——为ASP.NET MVC应用程序使用高级功能 为ASP.NET MVC应用程序使用高级功能这是微软官方教程Getting Starte ...

  4. 树莓派的媒体播放软件omxplayer

    树莓派中的CPU性能较差,而GPU较强大.omxplayer是专门针对树莓派的GPU的播放器.( made by Edgar (gimli) Hucek from the XBMC/Kodi proj ...

  5. JMeter:生成漂亮的多维度的HTML报告

    JMeter:生成漂亮的多维度的HTML报告我们做性能测试的时候会经常使用一些性能测试工具,我个人比较喜欢Jmeter这个工具,但是JMeter这个工具在生成测试报告方面一直有所欠缺.但是JMeter ...

  6. mac os 里的 JAVA_HOME

    google了一下,发现了这篇文章Important Java Directories on Mac OS X(https://developer.apple.com/library/content/ ...

  7. telinit:Did not receive a reply.Possible causes include:the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired

    问题: Enabling /etc/fstab swaps: [ok]telinit:Did not receive a reply.Possible causes include:the remot ...

  8. windows 安装操作系统时切换分区表格式

    在出现分区管理界面时,按下shift+F10呼出命令行,输入diskpart 后尝试如下命令将磁盘分区表手动转换到MBR. list disk ---- 显示当前磁盘列表 select disk x ...

  9. 汇编,浮点运算符,fldpi,fmul等指令说明.

    协处理器指令系统 协处理器共有68条不同的指令,汇编程序在遇到协处理器指令助记符时,都会将其转换成机器语言的ESC指令,ESC指令代表了协处理器的操作码. 协处理器指令在执行过程中,需要访问内存单元时 ...

  10. [转]LAMP(Linux-Apache-MySQL-PHP)网站架构

    本文转自 http://www.williamlong.info/archives/1908.html LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框 ...