★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-kdvjoclt-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We are to write the letters of a given string S
, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths
, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'.git
Now answer two questions: how many lines have at least one character from S
, and what is the width used by the last such line? Return your answer as an integer list of length 2. github
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:數組
S
will be in the range [1, 1000].S
will only contain lowercase letters.widths
is an array of length 26
.widths[i]
will be in the range of [2, 10]
. 咱們要把給定的字符串 S
從左到右寫到每一行上,每一行的最大寬度爲100個單位,若是咱們在寫某個字母的時候會使這行超過了100 個單位,那麼咱們應該把這個字母寫到下一行。咱們給定了一個數組 widths
,這個數組 widths[0] 表明 'a' 須要的單位, widths[1] 表明 'b' 須要的單位,..., widths[25] 表明 'z' 須要的單位。微信
如今回答兩個問題:至少多少行能放下S
,以及最後一行使用的寬度是多少個單位?將你的答案做爲長度爲2的整數列表返回。spa
示例 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個單位寬度。
注:code
S
的長度在 [1, 1000] 的範圍。S
只包含小寫字母。widths
是長度爲 26
的數組。widths[i]
值的範圍在 [2, 10]
。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 }
8mshtm
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 }
12msblog
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 msci
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 }