加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Veri

发布时间:2020-12-14 05:09:16 所属栏目:百科 来源:网络整理
导读:In an alien language,surprisingly they also use english lowercase letters,but possibly?in a different? order . The? order ?of the alphabet?is some permutation?of lowercase letters. Given a sequence of? words ?written in the alien language,

In an alien language,surprisingly they also use english lowercase letters,but possibly?in a different?order. The?order?of the alphabet?is some permutation?of lowercase letters.

Given a sequence of?words?written in the alien language,?and the?order?of the alphabet,?return?true?if and only if the given?words?are sorted lexicographicaly in this alien language.

Example 1:

Input: words = ["hello","leetcode"],order = "hlabcdefgijkmnopqrstuvwxyz" Output: true Explanation: As ‘h‘ comes before ‘l‘ in this language,then the sequence is sorted. 

Example 2:

Input: words = ["word","world","row"],order = "worldabcefghijkmnpqstuvxyz" Output: false Explanation: As ‘d‘ comes after ‘l‘ in this language,then words[0] > words[1],hence the sequence is unsorted. 

Example 3:

Input: words = ["apple","app"],order = "abcdefghijklmnopqrstuvwxyz" Output: false Explanation: The first three characters "app" match,and the second string is shorter (in size.) According to lexicographical rules "apple" > "app",because ‘l‘ > ‘?‘,where ‘?‘ is defined as the blank character which is less than any other character (More info).

Note:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters in?words[i]?and?order?are english lowercase letters.

某种外星语也使用英文小写字母,但可能顺序?order?不同。字母表的顺序(order)是一些小写字母的排列。

给定一组用外星语书写的单词?words,以及其字母表的顺序?order,只有当给定的单词在这种外星语中按字典序排列时,返回?true;否则,返回?false

示例 1:

输入:words = ["hello",order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,‘h‘ 位于 ‘l‘ 之前,所以单词序列是按字典序排列的。

示例 2:

输入:words = ["word",order = "worldabcefghijkmnpqstuvxyz"
输出:false
解释:在该语言的字母表中,‘d‘ 位于 ‘l‘ 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。

示例 3:

输入:words = ["apple",order = "abcdefghijklmnopqrstuvwxyz"
输出:false
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 ‘l‘ > ‘?‘,其中 ‘?‘ 是空白字符,定义为比任何其他字符都小(更多信息)。

提示:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. 在?words[i]?和?order?中的所有字符都是英文小写字母。

72ms
 1 class Solution {
 2     func isAlienSorted(_ words: [String],_ order: String) -> Bool {
 3         var words = words
 4         var a:[Int] = [Int](repeating:0,count:256)
 5         var i:Int = 0
 6         var j:Int = 0
 7         for i in 0..<order.count
 8         {
 9             a[order.toInt(i)] = i
10         }
11         for i in 0..<words.count
12         {
13             for j in 0..<words[i].count
14             {
15                 //a:97
16                 words[i][j] = (a[words[i][j].ascii] + 97).ASCII
17             }
18         }
19          for i in 0..<(words.count - 1)
20         {
21             if words[i] > words[i+1]
22             {
23                 return false
24             }
25         }
26         return true
27     }
28 }
29 
30 extension String {    
31     //获取指定索引位置的字符,返回为字符串形式
32     func charAt(_ num:Int) -> String
33     {
34         return String(self[index(self.startIndex,offsetBy: num)]) 
35     }
36     
37     //获取指定索引位置字符的ASCII整数值
38     func toInt(_ num:Int) -> Int
39     {
40         let s = charAt(num).unicodeScalars
41         return Int(s[s.startIndex].value) 
42     }
43     
44     //subscript函数可以检索数组中的值
45     //直接按照索引方式截取指定索引的字符
46     subscript (_ i: Int) -> Character {
47         //读取字符
48         get {return self[index(startIndex,offsetBy: i)]}
49         
50         //修改字符
51         set
52         {
53             var str:String = self
54             var index = str.index(startIndex,offsetBy: i)
55             str.remove(at: index)
56             str.insert(newValue,at: index)
57             self = str
58         }
59     }
60 }
61 
62 //Character扩展方法  
63 extension Character  
64 {  
65   //属性:ASCII整数值(定义小写为整数值)
66    var ascii: Int {
67         get {
68             let s = String(self).unicodeScalars
69             return Int(s[s.startIndex].value)
70         }
71     }
72 }
73 
74 //Int扩展方法  
75 extension Int
76 {
77     //属性:ASCII值(定义大写为字符值)
78     var ASCII:Character 
79     {
80         get {return Character(UnicodeScalar(self)!)}
81     }
82 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读