Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift Weekly Contest 110]LeetCode937. 重新排列日志文件 | 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 co
@H_618_10@

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

  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.

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

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

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

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

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

返回日志的最终顺序。

示例 :

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

提示

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. 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..<n
18         {
19             ret[i] = ss[i][2]
20         }
21         return ret
22     }
23     
24     func compare(_ a:[String],_ b:[String]) -> Bool
25     {
26         //‘0‘:ASCII码值 48; ‘9‘ASCII码值 57
27         var num1:Int = a[0].toInt(0)
28         var ad:Bool = num1 >= 48 && num1 <= 57
29         
30         var num2:Int = b[0].toInt(0)
31         var bd:Bool = num2 >= 48 && num2 <= 57
32         
33         if !ad && bd{return true}
34         if ad && !bd{return false}
35         var str1:string
36         var str2:string
37         if !ad && !bd
38         {
39             if a[0] != b[0]
40             {
41                 str1 = a[0]
42                 str2 = b[0]
43             }
44             else
45             {
46                 str1 = a[1]
47                 str2 = b[1]
48             }
49         }
50         else
51         {
52             str1 = a[3]
53             str2 = b[3]
54         }
55         let num = str1.caseInsensitiveCompare(str2).rawValue
56         if num == -1
57         {
58             return true
59         }
60         return false
61     }
62 }
63 
64 extension String {
65     //获取指定索引位置的字符,返回为字符串形式
66     func charAt(_ num:int) -> String
67     {
68         return String(self[index(self.starTindex,offsetBy: num)]) 
69     }
70     
71     //获取指定索引位置字符的ASCII整数值
72     func toInt(_ num:int) -> Int
73     {
74         let s = charAt(num).unicodeScalars
75         return Int(s[s.starTindex].value) 
76     }
77 }

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。