File tree 3 files changed +129
-8
lines changed
solution/0000-0099/0068.Text Justification
3 files changed +129
-8
lines changed Original file line number Diff line number Diff line change @@ -90,10 +90,50 @@ maxWidth = 20
90
90
91
91
```
92
92
93
- ### ** ...**
94
-
95
- ```
96
-
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ vector<string > fullJustify(vector<string >& words, int maxWidth) {
99
+ int n = words.size();
100
+ vector<string > result;
101
+ for (int i = 0; i < n; i++)
102
+ {
103
+ int begin = i;
104
+ int wordLen = words[ i] .size();
105
+ while (i + 1 < n && words[ i + 1] .size() + wordLen + 1 <= maxWidth)
106
+ {
107
+ wordLen += words[ ++i] .size() + 1;
108
+ }
109
+ int numberofWords = i - begin + 1;
110
+ int space = 1;
111
+ int extraSpace = 0;
112
+ if (numberofWords > 1 && i < n - 1)
113
+ {
114
+ int remaining = maxWidth - wordLen;
115
+ space = remaining / (numberofWords - 1) + 1;
116
+ extraSpace = remaining % (numberofWords - 1);
117
+ }
118
+ string line = words[ begin] ;
119
+ for (int j = 1; j < numberofWords; j++)
120
+ {
121
+ line.append(space, ' ');
122
+ if (j <= extraSpace)
123
+ {
124
+ line.push_back(' ');
125
+ }
126
+ line += words[ begin + j] ;
127
+ }
128
+ if (line.size() < maxWidth)
129
+ {
130
+ line.append(maxWidth - line.size(), ' ');
131
+ }
132
+ result.emplace_back(line);
133
+ }
134
+ return result;
135
+ }
136
+ };
97
137
```
98
138
99
139
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -86,10 +86,50 @@ Note that the second line is also left-justified becase it contains only one wor
86
86
87
87
```
88
88
89
- ### ** ...**
90
-
91
- ```
92
-
89
+ ### ** C++**
90
+
91
+ ``` cpp
92
+ class Solution {
93
+ public:
94
+ vector<string > fullJustify(vector<string >& words, int maxWidth) {
95
+ int n = words.size();
96
+ vector<string > result;
97
+ for (int i = 0; i < n; i++)
98
+ {
99
+ int begin = i;
100
+ int wordLen = words[ i] .size();
101
+ while (i + 1 < n && words[ i + 1] .size() + wordLen + 1 <= maxWidth)
102
+ {
103
+ wordLen += words[ ++i] .size() + 1;
104
+ }
105
+ int numberofWords = i - begin + 1;
106
+ int space = 1;
107
+ int extraSpace = 0;
108
+ if (numberofWords > 1 && i < n - 1)
109
+ {
110
+ int remaining = maxWidth - wordLen;
111
+ space = remaining / (numberofWords - 1) + 1;
112
+ extraSpace = remaining % (numberofWords - 1);
113
+ }
114
+ string line = words[ begin] ;
115
+ for (int j = 1; j < numberofWords; j++)
116
+ {
117
+ line.append(space, ' ');
118
+ if (j <= extraSpace)
119
+ {
120
+ line.push_back(' ');
121
+ }
122
+ line += words[ begin + j] ;
123
+ }
124
+ if (line.size() < maxWidth)
125
+ {
126
+ line.append(maxWidth - line.size(), ' ');
127
+ }
128
+ result.emplace_back(line);
129
+ }
130
+ return result;
131
+ }
132
+ };
93
133
```
94
134
95
135
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<string> fullJustify (vector<string>& words, int maxWidth) {
4
+ int n = words.size ();
5
+ vector<string> result;
6
+ for (int i = 0 ; i < n; i++)
7
+ {
8
+ int begin = i;
9
+ int wordLen = words[i].size ();
10
+ while (i + 1 < n && words[i + 1 ].size () + wordLen + 1 <= maxWidth)
11
+ {
12
+ wordLen += words[++i].size () + 1 ;
13
+ }
14
+ int numberofWords = i - begin + 1 ;
15
+ int space = 1 ;
16
+ int extraSpace = 0 ;
17
+ if (numberofWords > 1 && i < n - 1 )
18
+ {
19
+ int remaining = maxWidth - wordLen;
20
+ space = remaining / (numberofWords - 1 ) + 1 ;
21
+ extraSpace = remaining % (numberofWords - 1 );
22
+ }
23
+ string line = words[begin];
24
+ for (int j = 1 ; j < numberofWords; j++)
25
+ {
26
+ line.append (space, ' ' );
27
+ if (j <= extraSpace)
28
+ {
29
+ line.push_back (' ' );
30
+ }
31
+ line += words[begin + j];
32
+ }
33
+ if (line.size () < maxWidth)
34
+ {
35
+ line.append (maxWidth - line.size (), ' ' );
36
+ }
37
+ result.emplace_back (line);
38
+ }
39
+ return result;
40
+ }
41
+ };
You can’t perform that action at this time.
0 commit comments