[leetcode] text-justification

时间限制:1秒 空间限制:32768K 热度指数:3907
本题知识点:  字符串  leetcode

 算法知识视频讲解

题目描述

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. Pad extra spaces' 'when necessary so that each line has exactly L 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.

For example,
words:["This", "is", "an", "example", "of", "text", "justification."]
L:16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
 
题解:
 1 #include<vector>
 2 #include<string>
 3 #include<sstream>
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class Solution {
 8 public:
 9     vector<string> fullJustify(vector<string> &words, int L) {
10         vector<string> res;
11         for (int i = 0, j, cnt = 0; i < words.size(); i = j) {
12             for (j = i; j < words.size() && cnt + words[j].length() <= L - (j - i); cnt += words[j].length(), j++);
13             ostringstream oss;
14             oss << words[i];
15             if (j - i == 1) {
16                 while (cnt++ < L) oss << ' ';
17             }
18             else if(j == words.size()){
19                 for (int t = i + 1; t < j; t++) {
20                     oss << ' ' << words[t];
21                 }
22                 while (cnt++ + (j - 1 - i) < L) oss << ' ';
23             }
24             else {
25                 string space = "";
26                 int spacecnt = L - cnt;
27                 int even = spacecnt / (j - 1 - i);
28                 int remainder = spacecnt % (j - 1 - i);
29                 while (even-- > 0) space += ' ';
30                 for (i = i + 1; i < j; i++) {
31                     oss << space;
32                     if (remainder-- > 0) oss << ' ';
33                     oss << words[i];
34                 }
35             }
36             cnt = 0;
37             res.push_back(oss.str());
38         }
39         return res;
40     }
41 };
42 
43 int main() {
44     Solution* solution = new Solution();
45     vector<string> words;
46     words.push_back("This");
47     words.push_back("is");
48     words.push_back("an");
49     words.push_back("example");
50     words.push_back("of");
51     words.push_back("text");
52     //words.push_back("justification.");
53     vector<string> res = solution->fullJustify(words, 16);
54     for (int i = 0; i < res.size(); i++) {
55         cout << "\"" << res[i] << "\"" << endl;
56     }
57     return 0;
58 }
 1 #include<vector>
 2 #include<string>
 3 #include<sstream>
 4 using namespace std;
 5 class Solution {
 6 public:
 7     vector<string> fullJustify(vector<string> &words, int L) {
 8         vector<string> res;
 9         int cnt = -1;
10         vector<string> temp;
11         for (int i = 0; i < words.size(); i++) {
12             if (cnt + words[i].length() + 1 <= L) {
13                 cnt += words[i].length() + 1;
14                 temp.push_back(words[i]);
15                 if (i == words.size() - 1) {
16                     ostringstream oss;
17                     oss << temp[0];
18                     for (int j = 1; j < temp.size(); j++) {
19                         oss << ' ';
20                         oss << temp[j];
21                     }
22                     string space = "";
23                     int ccnt = L - cnt;
24                     while (ccnt-- > 0) {
25                         space += ' ';
26                     }
27                     oss << space;
28                     res.push_back(oss.str());
29                 }
30             }
31             else {
32                 if (temp.size() == 1) {
33                     int spacenum = L - temp[0].length();
34                     ostringstream oss;
35                     oss << temp[0];
36                     while (spacenum-- > 0) {
37                         oss << ' ';
38                     }
39                     res.push_back(oss.str());
40                 }
41                 else {
42                     int spacenum = L - cnt + (temp.size() - 1);
43                     int remainder = spacenum % (temp.size() - 1);
44                     int even = spacenum / (temp.size() - 1);
45                     ostringstream oss;
46                     string space = "";
47                     while (even-- > 0) {
48                         space += ' ';
49                     }
50                     oss << temp[0];
51                     for (int j = 1; j < temp.size(); j++) {
52                         oss << space;
53                         if (remainder-- > 0) oss << ' ';
54                         oss << temp[j];
55                     }
56                     res.push_back(oss.str());
57                 }
58                 cnt = -1;
59                 temp.clear();
60                 i--;
61             }
62         }
63         return res;
64     }
65 };

猜你喜欢

转载自www.cnblogs.com/yfzhou/p/9689093.html
今日推荐