日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode-937-Reorder Log Files-(Easy)

發布時間:2025/3/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 <= 100
  • 3 <= logs[i].length <= 100
  • 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('') ,表示尋找某個字符所在的位置

    ?

    轉載于:https://www.cnblogs.com/doudouyoutang/p/10189345.html

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的Leetcode-937-Reorder Log Files-(Easy)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。