加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:10:11 所属栏目:百科 来源:网络整理
导读: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 yo

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","what","we","understand","well","enough","to","explain",?        "to","a","computer.","Art","everything","else","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","justification."]
maxWidth = 16
输出:
[
? ?"This ? ?is ? ?an",? ?"justification. ?"
]

示例?2:

输入:
words = ["What","be"]
maxWidth = 16
输出:
[
? "What ? must ? be",? "shall be ? ? ? ?"
]
解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be",?    因为最后一行应为左对齐,而不是左右两端对齐。       
     第二行同样为左对齐,这是因为这行只包含一个单词。

示例?3:

输入:
words = ["Science","do"]
maxWidth = 20
输出:
[
? "Science ?is ?what we",? "do ? ? ? ? ? ? ? ? ?"
]

本题待优化
 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,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],_ 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 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读