diff --git a/solution/0000-0099/0068.Text Justification/README.md b/solution/0000-0099/0068.Text Justification/README.md index c8e544b021c4a..a17b40590e281 100644 --- a/solution/0000-0099/0068.Text Justification/README.md +++ b/solution/0000-0099/0068.Text Justification/README.md @@ -90,10 +90,50 @@ maxWidth = 20 ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + int n = words.size(); + vector result; + for (int i = 0; i < n; i++) + { + int begin = i; + int wordLen = words[i].size(); + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) + { + wordLen += words[++i].size() + 1; + } + int numberofWords = i - begin + 1; + int space = 1; + int extraSpace = 0; + if (numberofWords > 1 && i < n - 1) + { + int remaining = maxWidth - wordLen; + space = remaining / (numberofWords - 1) + 1; + extraSpace = remaining % (numberofWords - 1); + } + string line = words[begin]; + for (int j = 1; j < numberofWords; j++) + { + line.append(space, ' '); + if (j <= extraSpace) + { + line.push_back(' '); + } + line += words[begin + j]; + } + if (line.size() < maxWidth) + { + line.append(maxWidth - line.size(), ' '); + } + result.emplace_back(line); + } + return result; + } +}; ``` diff --git a/solution/0000-0099/0068.Text Justification/README_EN.md b/solution/0000-0099/0068.Text Justification/README_EN.md index ccde085caf56f..4740f07b7946c 100644 --- a/solution/0000-0099/0068.Text Justification/README_EN.md +++ b/solution/0000-0099/0068.Text Justification/README_EN.md @@ -86,10 +86,50 @@ Note that the second line is also left-justified becase it contains only one wor ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + int n = words.size(); + vector result; + for (int i = 0; i < n; i++) + { + int begin = i; + int wordLen = words[i].size(); + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) + { + wordLen += words[++i].size() + 1; + } + int numberofWords = i - begin + 1; + int space = 1; + int extraSpace = 0; + if (numberofWords > 1 && i < n - 1) + { + int remaining = maxWidth - wordLen; + space = remaining / (numberofWords - 1) + 1; + extraSpace = remaining % (numberofWords - 1); + } + string line = words[begin]; + for (int j = 1; j < numberofWords; j++) + { + line.append(space, ' '); + if (j <= extraSpace) + { + line.push_back(' '); + } + line += words[begin + j]; + } + if (line.size() < maxWidth) + { + line.append(maxWidth - line.size(), ' '); + } + result.emplace_back(line); + } + return result; + } +}; ``` diff --git a/solution/0000-0099/0068.Text Justification/Solution.cpp b/solution/0000-0099/0068.Text Justification/Solution.cpp new file mode 100644 index 0000000000000..f19ab8bcd7582 --- /dev/null +++ b/solution/0000-0099/0068.Text Justification/Solution.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + int n = words.size(); + vector result; + for (int i = 0; i < n; i++) + { + int begin = i; + int wordLen = words[i].size(); + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) + { + wordLen += words[++i].size() + 1; + } + int numberofWords = i - begin + 1; + int space = 1; + int extraSpace = 0; + if (numberofWords > 1 && i < n - 1) + { + int remaining = maxWidth - wordLen; + space = remaining / (numberofWords - 1) + 1; + extraSpace = remaining % (numberofWords - 1); + } + string line = words[begin]; + for (int j = 1; j < numberofWords; j++) + { + line.append(space, ' '); + if (j <= extraSpace) + { + line.push_back(' '); + } + line += words[begin + j]; + } + if (line.size() < maxWidth) + { + line.append(maxWidth - line.size(), ' '); + } + result.emplace_back(line); + } + return result; + } +};