[Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)

➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/

➤GitHub地址:https://github.com/strengthen/LeetCode

➤原文地址: https://www.cnblogs.com/strengthen/p/10548894.html

➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。

➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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'.

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.

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:

  • The length of 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的整数列表返回。

示例 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个单位宽度。 

注:

  • 字符串 S 的长度在 [1, 1000] 的范围。
  • S 只包含小写字母。
  • widths 是长度为 26的数组。
  • widths[i] 值的范围在 [2, 10]

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 }