leetcode937
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的更多相关文章
- [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 ...
- Leetcode937. Reorder Log Files重新排列日志文件
你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...
随机推荐
- 差分约束+spfa【模板】
相比dij,spfa优点是可处理含负边不含负圈的最短路问题,缺点是算法复杂度不太好[貌似可以使用两种优化.LLL和SLF] 差分约束就是将一些不等式转化为图中的带权边,然后求解最短路或最长路的方法 洛 ...
- 【问题】PPS、PPSX自动放映格式打开直接进入编辑模式
在做自动放映格式的PPT的时候,发现另存为PPS或PPSX格式后,自动放映无法实现,而是直接进入了PPT编辑模式,于是开始寻找原因.发现是文件关联有问题,这与安装多个版本的ppt有关系. 解决办法: ...
- ZH奶酪:Python 中缀表达式转换后缀表达式
实现一个可以处理加减乘数运算的中缀表达式转换后缀表达式的程序: 一个输入中缀表达式inOrder 一个输出池pool 一个缓存栈stack 从前至后逐字读取inOrder 首先看一下不包含括号的: ( ...
- [C++ Primer] : 第14章: 重载运算符与类型转换
基本概念 重载运算符是具有特殊名字的函数: 它们的名字由关键字operator和其后要定义的运算符号共同组成. 重载运算符函数的参数数量与该运算符作用的运算对象数量一样多. 对于二元运算符来说, 左侧 ...
- linux sh文件提示 no such file or directory
Linux执行.sh文件,提示No such file or directory的问题的解决方法 12-06-28 16:59作者:love__coder Linux执行.sh文件,提示No such ...
- android 获取当前 activity
ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE); List<RunningTask ...
- busybox下的tftp client
# tftp -p -l file host # put local file to remote host # tftp -g -r file host # get remote fil ...
- CAP原理和BASE思想--GLQ
分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能Partition tolerance(分区容忍性) 可 ...
- aapium 设置安卓机参数
例子: class iBer(Unittest.TestCase): @classmethod def setUpClass(cls): logger=public.log() desired_cap ...
- 杂项:Mantis
ylbtech-杂项:Mantis 缺陷管理平台Mantis,也做MantisBT,全称Mantis Bug Tracker.Mantis是一个基于PHP技术的轻量级的开源缺陷跟踪系统,以Web操作的 ...