Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' 
when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. 
If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:
A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.

class Solution:
    # @param words, a list of strings
    # @param L, an integer
    # @return a list of strings
    def fullJustify(self, words, L):
        cur_len = 0
        ret = []
        res = []
        
        for word in words:
            if cur_len + len(word) + len(res) <= L:  # Note 1
                cur_len += len(word)
                res.append(word)
            else:
                if len(res) == 1:
                    ret.append(self.fill_spaces(res[0],L)) # Note 7
                else:
                    extra_space = L - cur_len - (len(res)-1) # Note 2
                    each_extra = extra_space/(len(res)-1) + 1
                    rest_spaces = extra_space % (len(res)-1)
                    for i in xrange(rest_spaces): # Note 3
                        res[i] += ' '
                    line = (' ' * each_extra).join(res) 
                    ret.append(line)
                res = []   # Note 4
                res.append(word) # Note 5
                cur_len = len(word)
        ret.append(self.fill_spaces(' '.join(res),L)) # Note 6
        return ret
    
    def fill_spaces(self, string, L):
        length = len(string)
        string += ' '* (L-length)
        return string 
                

# Notes:
# 1. 很好的思路，use cur_len + len(word) + len(res) 去判断
# 2. extra_space 去除了每个单词之间必须有的空格
#    each_extra 加上了每个单词之间必须有的空格
#    rest_spaces 除了每个单词之间必须有的空格和多余的空格，还剩下多少空格需要填补
# 3. 注意思想，如何加上多余的空格
# 4. after one loop, the res need to clean
# 5. Add the last word, for example here is "example", could not add into the first line,
#    add into the next line 
# 6. 最后是一定会append多余的一行的,没必要再check了, 直接append, 但是这里需要用
#    ' '.join, not the ''.join
# 7. 需要check长度为1，因为在下面计算中len(res)-1

@@ -1796,16 +1796,25 @@
 (res)-1)
+ # Note 8
 %0A       
@@ -1959,16 +1959,25 @@
 in(res) 
+ %EF%BC%83 Note 9
 %0A       
@@ -2826,8 +2826,362 @@
 (res)-1%0A
+# 8. %E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%E8%A6%81%E7%94%A8 extra_space%E5%8E%BB%E9%99%A4%E4%BB%A5%0A# 9. %E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%E4%B8%8D%E8%83%BD%E7%94%A8%E4%BB%A5%E4%B8%8B%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%8C%E4%BC%9A%E5%87%BA%E7%8E%B0%E9%97%AE%E9%A2%98 %5B'a b ', 'c d '%5D%EF%BC%8Ctest case: %5B'a','b','c','d','e','f'%5D, 3%0A#                   # substring = ''%0A                    # for ele in res:%0A                    #     substring += ele + ' '*each_extra %0A                    # result.append(substring)%0A                    # %E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%E5%BF%85%E9%A1%BB%E7%94%A8join%EF%BC%8C%E4%B8%8D%E5%8F%AF%E4%BB%A5%E7%94%A8%E5%BE%AA%E7%8E%AF%EF%BC%8C%E4%BC%9A%E5%87%BA%E7%8E%B0%5B'a b ','c d '%5D%E7%9A%84%E6%83%85%E5%86%B5%0A

Update Text_Justification.py