[Swift]LeetCode68. 文本左右对齐 | Text Justification

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 }

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

时间: 2024-11-06 03:56:20

[Swift]LeetCode68. 文本左右对齐 | Text Justification的相关文章

Text Justification,文本对齐

问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", "is", "an", "example", "of", "text", "justification."]L: 16. Return the formatted lines as: [ &

Leetcode 68. Text Justification 文本调整 解题报告

1 解题思想 这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子. 要求有: 1.每一行的字符串长度不能超过一个固定长度maxWidth 2.每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少.并且,空格多的地方只比右边少的多一个 3.最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好 提前贴个解释: * 这道题关键在于仔细的处理每一个步骤: * 1.每一行选择K的单词,K个

[leetcode]68. Text Justification文字对齐

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

LeetCode --- 68. Text Justification

题目链接:Text Justification Given an array of words and a length L, format the text such that each line has exactly L 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

No.68 Text Justification

No.68 Text Justification Given an array of words and a length L, format the text such that each line has exactly L 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

【一天一道LeetCode】#68. Text Justification

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack

Leetcode 68.文本左右对齐

文本左右对齐 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用"贪心算法"来放置给定的单词:也就是说,尽可能多地往每行中放置单词.必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符. 要求尽可能均匀分配单词间的空格数量.如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数. 文本的最后一行应为左对齐,且单词之间不插入额外的空格. 说明: 单词是指由非空格字

【leetcode】Text Justification

Text Justification Given an array of words and a length L, format the text such that each line has exactly L 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 i

LeetCode: Text Justification [068]

[题目] Given an array of words and a length L, format the text such that each line has exactly L 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. P