1. 程式人生 > >[Swift]LeetCode68. 文本左右對齊 | Text Justification

[Swift]LeetCode68. 文本左右對齊 | Text Justification

ant div 示例 etc style exactly str 運算符 ins

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ‘ ‘ when necessary so that each line has exactly maxWidth

characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word‘s length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
             because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

給定一個單詞數組和一個長度 maxWidth,重新排版單詞,使其成為每行恰好有 maxWidth 個字符,且左右兩端對齊的文本。

你應該使用“貪心算法”來放置給定的單詞;也就是說,盡可能多地往每行中放置單詞。必要時可用空格 ‘ ‘ 填充,使得每行恰好有 maxWidth 個字符。

要求盡可能均勻分配單詞間的空格數量。如果某一行單詞間的空格不能均勻分配,則左側放置的空格數要多於右側的空格數。

文本的最後一行應為左對齊,且單詞之間不插入額外的空格。

說明:

  • 單詞是指由非空格字符組成的字符序列。
  • 每個單詞的長度大於 0,小於等於 maxWidth
  • 輸入單詞數組 words 至少包含一個單詞。

示例:

輸入:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
輸出:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

示例 2:

輸入:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
輸出:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
解釋: 註意最後一行的格式應為 "shall be    " 而不是 "shall     be",
     因為最後一行應為左對齊,而不是左右兩端對齊。       
     第二行同樣為左對齊,這是因為這行只包含一個單詞。

示例 3:

輸入:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
輸出:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

轉義的C++代碼但是執行有誤
 1 class Solution {
 2     func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
 3         var res:[String] = [String]()
 4         var i:Int = 0
 5         while(i < words.count)
 6         {
 7             var j:Int = i, len:Int = 0
 8             while (j < words.count && len + words[j].count + j - i <= maxWidth)
 9             {
10                 len += words[j++].count
11             }
12             var out:String = String()
13             var space:Int = maxWidth - len
14             for k in i..<j
15             {
16                 out += words[k]
17                 if space > 0
18                 {
19                     var tmp:Int
20                     if j == words.count
21                     {
22                          if j - k == 1 {tmp = space}
23                          else {tmp = 1}
24                         
25                     }
26                     else
27                     {
28                         if j - k - 1 > 0
29                         {
30                             if space % (j - k - 1) == 0
31                             {
32                                 tmp = space / (j - k - 1)
33                             }
34                             else
35                             {
36                                 tmp = space / (j - k - 1) + 1
37                             }
38                         }
39                         else
40                         {
41                             tmp = space
42                         }
43                     }
44                     out = out.appendString(tmp, " ")
45                     space -= tmp
46                 }
47             } 
48             res.append(out)
49             i = j
50         }
51         return res
52     }
53 }
54 
55 extension String {
56     
57     func appendString(_ temp:Int,_ str:String) -> String
58     {
59         var strings = self
60         for _ in 0..<temp
61         {
62             strings.append(str)
63         }
64         return strings
65     }
66 }
67 /*擴展Int類,實現自增++、自減--運算符*/
68 extension Int{
69     //後綴++:先執行表達式後再自增
70     static postfix func ++(num:inout Int) -> Int {
71         //輸入輸出參數num
72         let temp = num
73         //num加1
74         num += 1
75         //返回加1前的數值
76         return temp
77     }
78 }

 1 class Solution {
 2     func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
 3         var res = [String]()
 4         var last = 0, currentLineLength = 0
 5         
 6         for (i, word) in words.enumerated() {
 7             if currentLineLength + word.count + (i - last) > maxWidth {
 8                 
 9                 res.append(buildLine(words, last, i - 1, maxWidth, currentLineLength))
10                 
11                 last = i
12                 currentLineLength = 0   
13             }
14             
15             currentLineLength += word.count
16         }
17         
18         res.append(buildLastLine(words, last, words.count - 1, maxWidth))
19         
20         return res
21     }
22     
23         fileprivate func buildLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int, _ currentLineLength: Int) -> String {
24         var line = ""
25         var extraSpaceNum = 0, spaceNum = 0
26         
27         if end > start {
28             extraSpaceNum = (maxWidth - currentLineLength) % (end - start)
29             spaceNum = (maxWidth - currentLineLength) / (end - start)
30         } else {
31             spaceNum = maxWidth - currentLineLength
32         }
33         
34         for i in start...end {
35             line.append(words[i])
36             
37             if start != end && i == end {
38                 break
39             } 
40             
41             for _ in 0..<spaceNum {
42                 line.append(" ")
43             }
44             
45             if extraSpaceNum > 0 {
46                 line.append(" ")
47                 extraSpaceNum -= 1
48             }
49         }
50         
51         return line
52     }
53     
54     fileprivate func buildLastLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int) -> String {
55         var line = ""
56         
57         for i in start...end {
58             line.append(words[i])
59             
60             if i < end {
61                 line.append(" ")
62             }
63         }
64         
65         while line.count < maxWidth {
66             line.append(" ")
67         }
68         
69         return line
70     }
71 
72 }

[Swift]LeetCode68. 文本左右對齊 | Text Justification