zoukankan      html  css  js  c++  java
  • leecode 937 Reorder Log Files (模拟)

    传送门:点我

    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.

    大意:给你一些日志,包括英文日志和数字日志,每个日志又包括日志头和日志内容,日志头是第一个单词,日志内容全数字的是数字日志,全英文的是英文日志。要求排序后输出。

    排序规则:对英文的日志,去掉日志头,按日志内容字典序排序。对数字的日志,按输入顺序。

                     总体上,英文日志在前,数字日志在后。

    思路:对每个字符串的最后一位进行判断之后,分类到两个向量里,对英语日志用stringstream进行分割,然后sort排序。

    代码:

    class Solution {
    public:
        static bool cmp(string s1,string s2){
            string news1="",news2="";
            stringstream ss1(s1);
            string s;
            int k = 0;
            while(ss1>>s){
                if(k > 0){
                    news1 = news1 + s +" "; 
                }
                k++;
            }
            k = 0;
            stringstream ss2(s2);
            while(ss2>>s){
                if(k > 0){
                    news2 = news2 + s +" "; 
                }
                k++;
            }
            if(news1<news2) return true;
               return false;
            //return news1 < news2;
        }
        vector<string> reorderLogFiles(vector<string>& logs) {
            vector<string>p1,p2,ans;
            for(int i = 0 ; i < logs.size() ; i++){
                string s = logs[i];
                if(s[s.size()-1]<='9' && s[s.size()-1] >= '0'){
                    p2.push_back(s);
                }
                else{
                    p1.push_back(s);
                }
            }
            sort(p1.begin(),p1.end(),cmp);
            for(int i = 0 ; i < p1.size() ; i ++){
                ans.push_back(p1[i]);
            }
            for(int i = 0 ; i < p2.size() ; i ++){
                ans.push_back(p2[i]);
            }
            return ans;
        }
    };
  • 相关阅读:
    Docker 国内镜像源
    SeMF安装指南
    CGI environment variables
    OpenResty + ngx_lua_waf使用
    OpenResty源码编译安装
    Ubuntu安装DVWA
    C安全编码实践
    [译]The Complete Application Security Checklist
    HTTP 安全头配置
    AWVS 10.5使用指南
  • 原文地址:https://www.cnblogs.com/Esquecer/p/10325217.html
Copyright © 2011-2022 走看看