[Swift Weekly Contest 110]LeetCode937. 重新排列日志文件 | Reorder Log Files

[Swift Weekly Contest 110]LeetCode937. 重新排列日志文件 | Reorder Log Files,第1张

概述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 co

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","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.

你有一个日志数组 logs。每条日志都是以空格分隔的字串。

对于每条日志,其第一个字为字母数字标识符。然后,要么:

标识符后面的每个字将仅由小写字母组成,或; 标识符后面的每个字将仅由数字组成。

我们将这两种日志分别称为字母日志和数字日志。保证每个日志在其标识符后面至少有一个字。

将日志重新排序,使得所有字母日志都排在数字日志之前。字母日志按字母顺序排序,忽略标识符,标识符仅用于表示关系。数字日志应该按原来的顺序排列

返回日志的最终顺序。

示例 :

输入:["a1 9 2 3 1","a8 act zoo"]输出:["g1 act car","zo4 4 7"]

提示:

0 <= logs.length <= 100 3 <= logs[i].length <= 100 logs[i] 保证有一个标识符,并且标识符后面有一个字。 120ms
 1 class Solution { 2     func reorderLogfiles(_ logs: [String]) -> [String] { 3         var n:Int = logs.count 4         var ss:[[String]] = [[String]](repeating: [String](),count: n) 5         for i in 0..<n 6         { 7             let str:String = logs[i] 8             var end = str.firstIndex(of: " ") ?? str.endindex 9             //不包括结束索引10             var ID:String = String(str[str.startIndex..<end])11             let start = end == str.endindex ? str.endindex : str.index(end,offsetBy: 1)12             var rem:String = String(str[start..<str.endindex])13             ss[i] = [rem,ID,str,String(format: "%05d",i)]14         }15         ss.sort(by: compare)16         var ret:[String] = [String] (repeating:String(),count: n)17         for i in 0..<n18         {19             ret[i] = ss[i][2]20         }21         return ret22     }23     24     func compare(_ a:[String],_ b:[String]) -> Bool25     {26         //‘0‘:ASCII码值 48; ‘9‘ASCII码值 5727         var num1:Int = a[0].toInt(0)28         var ad:Bool = num1 >= 48 && num1 <= 5729         30         var num2:Int = b[0].toInt(0)31         var bd:Bool = num2 >= 48 && num2 <= 5732         33         if !ad && bd{return true}34         if ad && !bd{return false}35         var str1:String36         var str2:String37         if !ad && !bd38         {39             if a[0] != b[0]40             {41                 str1 = a[0]42                 str2 = b[0]43             }44             else45             {46                 str1 = a[1]47                 str2 = b[1]48             }49         }50         else51         {52             str1 = a[3]53             str2 = b[3]54         }55         let num = str1.caseInsensitiveCompare(str2).rawValue56         if num == -157         {58             return true59         }60         return false61     }62 }63 64 extension String {65     //获取指定索引位置的字符,返回为字符串形式66     func charat(_ num:Int) -> String67     {68         return String(self[index(self.startIndex,offsetBy: num)]) 69     }70     71     //获取指定索引位置字符的ASCII整数值72     func toInt(_ num:Int) -> Int73     {74         let s = charat(num).unicodeScalars75         return Int(s[s.startIndex].value) 76     }77 }
总结

以上是内存溢出为你收集整理的[Swift Weekly Contest 110]LeetCode937. 重新排列日志文件 | Reorder Log Files全部内容,希望文章能够帮你解决[Swift Weekly Contest 110]LeetCode937. 重新排列日志文件 | Reorder Log Files所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1022393.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存