在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We are to write the letters of a given string Now answer two questions: how many lines have at least one character from Example : Input: widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] S = "abcdefghijklmnopqrstuvwxyz" Output: [3, 60] Explanation: All letters have the same length of 10. To write all 26 letters, we need two full lines and one line with 60 units. Example : Input: widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] S = "bbbcccdddaaa" Output: [2, 4] Explanation: All letters except 'a' have the same length of 10, and "bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units. For the last 'a', it is written on the second line because there is only 2 units left in the first line. So the answer is 2 lines, plus 4 units in the second line. Note:
我们要把给定的字符串 现在回答两个问题:至少多少行能放下 示例 1: 输入: widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] S = "abcdefghijklmnopqrstuvwxyz" 输出: [3, 60] 解释: 所有的字符拥有相同的占用单位10。所以书写所有的26个字母, 我们需要2个整行和占用60个单位的一行。 示例 2: 输入: widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] S = "bbbcccdddaaa" 输出: [2, 4] 解释: 除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位. 最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。 所以,这个答案是2行,第二行有4个单位宽度。 注:
Runtime: 8 ms
Memory Usage: 20 MB
1 class Solution { 2 func numberOfLines(_ widths: [Int], _ S: String) -> [Int] { 3 var dic:[Character:Int] = Dictionary.init() 4 let characters = Array.init("abcdefghijklmnopqrstuvwxyz") 5 for (i,value) in widths.enumerated() { 6 dic[characters[i]] = value 7 } 8 var result = 0 9 var hang = 1 10 for c in S { 11 let a = result + dic[c]! 12 if a <= 100 { 13 result += dic[c]! 14 } else { 15 result = dic[c]! 16 hang += 1 17 } 18 } 19 return [hang,result] 20 } 21 } 8ms 1 class Solution { 2 func numberOfLines(_ widths: [Int], _ S: String) -> [Int] { 3 let chars = Array(S) 4 var sum = 0 5 var l = 0 6 var last = 0 7 for c in chars { 8 let diff = widths[Int(c.unicodeScalars.first!.value - Character("a").unicodeScalars.first!.value)] 9 sum += diff 10 if sum > 100 { 11 l += 1 12 last = diff 13 sum = diff 14 } else if sum == 100 { 15 l += 1 16 last = diff 17 sum = 0 18 } else { 19 last = sum 20 } 21 } 22 return [l+1,last] 23 } 24 } 12ms 1 class Solution { 2 func numberOfLines(_ widths: [Int], _ S: String) -> [Int] { 3 var currentWidth = 0 4 var lines = 1 5 let a = "a".unicodeScalars.first!.value 6 for letter in S.unicodeScalars { 7 let width = widths[Int(letter.value - a)] 8 if currentWidth + width > 100 { 9 lines += 1 10 currentWidth = 0 11 } 12 currentWidth += width 13 } 14 return [lines, currentWidth] 15 } 16 } 32 ms 1 class Solution { 2 func numberOfLines(_ widths: [Int], _ S: String) -> [Int] { 3 var cnt:Int = 1 4 var cur:Int = 0 5 for c in S 6 { 7 var t:Int = widths[c.ascii - 97] 8 if cur + t > 100 9 { 10 cnt += 1 11 } 12 cur = (cur + t > 100) ? t : cur + t 13 } 14 return [cnt, cur] 15 } 16 } 17 18 //Character扩展 19 extension Character 20 { 21 //Character转ASCII整数值(定义小写为整数值) 22 var ascii: Int { 23 get { 24 return Int(self.unicodeScalars.first?.value ?? 0) 25 } 26 } 27 }
|
请发表评论