From 55ad6e00fb791af5a9bc41965793c8f1eb6c162e Mon Sep 17 00:00:00 2001 From: AldrinMarz7! <53973174+Dhoni77@users.noreply.github.com> Date: Wed, 9 Mar 2022 14:15:10 +0530 Subject: [PATCH 1/8] 0068-Text Justification Added cpp code in readme --- .../0068.Text Justification/README_EN.md | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README_EN.md b/solution/0000-0099/0068.Text Justification/README_EN.md index ccde085caf56f..e4a0ecb3a498d 100644 --- a/solution/0000-0099/0068.Text Justification/README_EN.md +++ b/solution/0000-0099/0068.Text Justification/README_EN.md @@ -86,10 +86,52 @@ Note that the second line is also left-justified becase it contains only one wor ``` -### **...** +### **C++** ``` - +class Solution { +public: +vector fullJustify(vector& words, int maxWidth) { + int n=words.size(); + vector result; + + for(int i=0;i 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 From 16bd4ca9e4bfce8be9eab964362717734e64a416 Mon Sep 17 00:00:00 2001 From: AldrinMarz7! <53973174+Dhoni77@users.noreply.github.com> Date: Wed, 9 Mar 2022 14:16:35 +0530 Subject: [PATCH 2/8] 0068-Text Justification Added cpp code --- .../0068.Text Justification/README.md | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README.md b/solution/0000-0099/0068.Text Justification/README.md index c8e544b021c4a..7ec76fc99b858 100644 --- a/solution/0000-0099/0068.Text Justification/README.md +++ b/solution/0000-0099/0068.Text Justification/README.md @@ -90,10 +90,52 @@ maxWidth = 20 ``` -### **...** +### **C++** ``` - +class Solution { +public: +vector fullJustify(vector& words, int maxWidth) { + int n=words.size(); + vector result; + + for(int i=0;i 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 From 02577f2d42ecc750c0e6794787a448c69aa46000 Mon Sep 17 00:00:00 2001 From: AldrinMarz7! <53973174+Dhoni77@users.noreply.github.com> Date: Wed, 9 Mar 2022 16:12:35 +0530 Subject: [PATCH 3/8] added cpp tag --- .../0068.Text Justification/README_EN.md | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README_EN.md b/solution/0000-0099/0068.Text Justification/README_EN.md index e4a0ecb3a498d..39b9430660a88 100644 --- a/solution/0000-0099/0068.Text Justification/README_EN.md +++ b/solution/0000-0099/0068.Text Justification/README_EN.md @@ -88,50 +88,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 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 fullJustify(vector < string > & words, int maxWidth) { + int n = words.size(); + vector < string > 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); } - return result; + + 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; } -} +} ``` From b3b9b48f7122b38c79d13dacf1171f7d91991216 Mon Sep 17 00:00:00 2001 From: AldrinMarz7! <53973174+Dhoni77@users.noreply.github.com> Date: Wed, 9 Mar 2022 16:13:23 +0530 Subject: [PATCH 4/8] added cpp tag --- .../0068.Text Justification/README.md | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README.md b/solution/0000-0099/0068.Text Justification/README.md index 7ec76fc99b858..83008a5ec7490 100644 --- a/solution/0000-0099/0068.Text Justification/README.md +++ b/solution/0000-0099/0068.Text Justification/README.md @@ -92,50 +92,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 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 fullJustify(vector < string > & words, int maxWidth) { + int n = words.size(); + vector < string > 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); } - return result; + + 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; } -} +} ``` From 29f53f40115324887b0d8fc5e8b17a860ee38150 Mon Sep 17 00:00:00 2001 From: AldrinMarz7! <53973174+Dhoni77@users.noreply.github.com> Date: Wed, 9 Mar 2022 16:21:54 +0530 Subject: [PATCH 5/8] Added Solution File --- .../0068.Text Justification/Solution.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 solution/0000-0099/0068.Text Justification/Solution.cpp 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..6aa27a69ae841 --- /dev/null +++ b/solution/0000-0099/0068.Text Justification/Solution.cpp @@ -0,0 +1,43 @@ +class Solution { + public: + vector < string > fullJustify(vector < string > & words, int maxWidth) { + int n = words.size(); + vector < string > 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; + } +} From 5ee4a0ff12c1bfca23ac234c8d889e601dc8dbaa Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Wed, 9 Mar 2022 18:59:56 +0800 Subject: [PATCH 6/8] Update README.md --- .../0068.Text Justification/README.md | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README.md b/solution/0000-0099/0068.Text Justification/README.md index 83008a5ec7490..a17b40590e281 100644 --- a/solution/0000-0099/0068.Text Justification/README.md +++ b/solution/0000-0099/0068.Text Justification/README.md @@ -94,48 +94,46 @@ maxWidth = 20 ```cpp class Solution { - public: - vector < string > fullJustify(vector < string > & words, int maxWidth) { - int n = words.size(); - vector < string > 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); +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); } - - 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; + return result; } -} +}; ``` From 2447d7ba852b77e1cdbb59068f8401c630e3f8cd Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Wed, 9 Mar 2022 19:00:10 +0800 Subject: [PATCH 7/8] Update README_EN.md --- .../0068.Text Justification/README_EN.md | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/README_EN.md b/solution/0000-0099/0068.Text Justification/README_EN.md index 39b9430660a88..4740f07b7946c 100644 --- a/solution/0000-0099/0068.Text Justification/README_EN.md +++ b/solution/0000-0099/0068.Text Justification/README_EN.md @@ -90,48 +90,46 @@ Note that the second line is also left-justified becase it contains only one wor ```cpp class Solution { - public: - vector < string > fullJustify(vector < string > & words, int maxWidth) { - int n = words.size(); - vector < string > 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); +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); } - - 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; + return result; } -} +}; ``` From 2948128bd2f5ac9cb5279563bc37e8155f684e39 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Wed, 9 Mar 2022 19:00:28 +0800 Subject: [PATCH 8/8] Update Solution.cpp --- .../0068.Text Justification/Solution.cpp | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/solution/0000-0099/0068.Text Justification/Solution.cpp b/solution/0000-0099/0068.Text Justification/Solution.cpp index 6aa27a69ae841..f19ab8bcd7582 100644 --- a/solution/0000-0099/0068.Text Justification/Solution.cpp +++ b/solution/0000-0099/0068.Text Justification/Solution.cpp @@ -1,43 +1,41 @@ class Solution { - public: - vector < string > fullJustify(vector < string > & words, int maxWidth) { - int n = words.size(); - vector < string > 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; +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); } - - 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; + return result; } -} +};