1. 程式人生 > >[Swift]LeetCode806. 寫字符串需要的行數 | Number of Lines To Write String

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

覆蓋 返回 ive 一行 問題 字母 字符串 each case

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 }

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