Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: codeyu/LeetCode
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Lz4Code/LeetCode-csharp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Nov 25, 2017

  1. Solution in C# and another reference code in python. I hope this helps .

    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 authored Nov 25, 2017
    Configuration menu
    Copy the full SHA
    1cc26f5 View commit details
    Browse the repository at this point in the history
Loading