Skip to content

Commit 86d221a

Browse files
authored
feat: add cpp solution to lc problem: No.0068 (#741)
No.0068. Text Justification
1 parent 3289f99 commit 86d221a

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

solution/0000-0099/0068.Text Justification/README.md

+44-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,50 @@ maxWidth = 20
9090

9191
```
9292

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+
};
97137
```
98138
99139
<!-- tabs:end -->

solution/0000-0099/0068.Text Justification/README_EN.md

+44-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,50 @@ Note that the second line is also left-justified becase it contains only one wor
8686

8787
```
8888

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+
};
93133
```
94134
95135
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
};

0 commit comments

Comments
 (0)