-
Notifications
You must be signed in to change notification settings - Fork 30
Solution in C# and another reference code in python. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution {
public IList<string> FullJustify(string[] words, int maxWidth) {
IList<string> res = new List<string>();
int i = 0;
int chLength = 0; //字符所占的长度
int wordLength = 0; //单词个数
int space = 0;
while(i < words.Length)
{
string word = words[i];
if(wordLength + chLength + word.Length <= maxWidth)
{
wordLength++;
chLength += word.Length;
i++;
continue;
}
space = maxWidth - chLength;
if(wordLength == 0) return res;
if(wordLength == 1)
{
res.Add(words[i - 1].PadRight(maxWidth, ' '));
}
else
{
int wordSpace = space / (wordLength - 1);
int extraSpace = space % (wordLength - 1);
int start = i - wordLength;
string cur = string.Empty;
for(int j = start; j < start + extraSpace; j++)
{
cur += words[j];
cur += new string(' ', wordSpace + 1);
}
for(int j = start + extraSpace; j < i - 1; j++)
{
cur += words[j];
cur += new string(' ', wordSpace);
}
cur += words[i - 1];
res.Add(cur);
}
chLength = 0;
wordLength = 0;
}
//加上最后一行
if(wordLength == 0) return res;
space = maxWidth - chLength;
if(wordLength == 1)
{
res.Add(words[i - 1].PadRight(maxWidth, ' '));
}
else
{
int start = i - wordLength;
string cur = string.Empty;
for(int j = start; j < i - 1; j++)
{
cur += words[j];
cur += ' ';
}
cur += words[i - 1];
res.Add(cur.PadRight(maxWidth, ' '));
}
return res;
}
}
class Solution:
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
res, cur, num_of_letters = [], [], 0
for w in words:
if num_of_letters + len(w) + len(cur) > maxWidth:
for i in range(maxWidth - num_of_letters):
cur[i%(len(cur)-1 or 1)] += ' '
res.append(''.join(cur))
cur, num_of_letters = [], 0
cur += [w]
num_of_letters += len(w)
return res + [' '.join(cur).ljust(maxWidth)]
@ralic
ralic
changed the title
(削除) Solution in C# and another reference code in python. I hope this helps . (削除ここまで)
(追記) Solution in C# and another reference code in python. (追記ここまで)
Nov 25, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#Python Ref