• 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:

    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.

      解释:

      给定一个字符串的数组,每个字符串中间用空格分割,第一个元素表示索引,后面的为内容;内容分为两种,一种是纯字符串,一种是纯数组

      要求进行排序,排序规则是,纯字符串内容的排在纯数字的前面;对于纯字符串的元素,比较索引大小排序;对于纯数字的其相对位置不变

      

    二、解答:

    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('') ,表示寻找某个字符所在的位置

  • 相关阅读:
    第一节:理解垃圾回收平台的基本工作原理
    回想笔记 瞎比比 域名注册 解析绑定ip 下载证书 设置证书 重定向http到https请求
    flask 设置https请求 访问flask服务器
    关于 服务器ip和域名进行一个绑定
    ubuntu 安装flask+nginx+gunicorn 待定
    使用flask-dropzone 上传图片文件
    flask 对于用户登录保持状态 flask_login
    flask 对于邮件url进行一个加密防止爆破
    flask 密码加密 视频资料
    flask 多线程邮件异步发送 视频资料
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10189345.html
Copyright © 2020-2023  润新知