GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/068_Text_Justification.py at master · gdsttian/leetcode

Expand file tree

/

068_Text_Justification.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
73 lines (71 loc) · 2.87 KB
/

068_Text_Justification.py

Copy path

File metadata and controls

73 lines (71 loc) · 2.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
res = []
res_list = []
curr = []
count, pos = 0, 0
while pos < len(words):
word = words[pos]
if len(word) > maxWidth:
pos += 1
if len(word) + count + len(curr)<= maxWidth:
count += len(word)
curr.append(word)
pos += 1
else:
res_list.append(curr)
curr = []
count = 0
if len(curr) > 0:
res_list.append(curr)
# print res_list
for index, curr in enumerate(res_list):
text = ''
remain = sum([len(t) for t in curr])
if len(curr) == 1:
# single word
text = curr[0] + ' ' * (maxWidth - remain)
elif index == len(res_list) - 1:
# last line
text = ' '.join(curr)
text += ' ' * (maxWidth - remain - len(curr) + 1)
else:
# multiple
step = (maxWidth - remain) / (len(curr) - 1 )
extra = (maxWidth - remain) % (len(curr) - 1 )
for index in range(len(curr) - 1):
text += curr[index] + ' ' * step
if extra > 0:
# assign from left
text += ' '
extra -= 1
text += curr[-1]
res.append(text)
return res
# def fullJustify(self, words, maxWidth):
# i, N, result = 0, len(words), []
# while i < N:
# # decide how many words to be put in one line
# oneLine, j, currWidth, positionNum, spaceNum = [words[i]], i + 1, len(words[i]), 0, maxWidth - len(words[i])
# while j < N and currWidth + 1 + len(words[j]) <= maxWidth:
# oneLine.append(words[j])
# currWidth += 1 + len(words[j])
# spaceNum -= len(words[j])
# positionNum, j = positionNum + 1, j + 1
# i = j
# # decide the layout of one line
# if i < N and positionNum:
# spaces = [' ' * (spaceNum / positionNum + (k < spaceNum % positionNum)) for k in range(positionNum)] + [
# '']
# else: # last line or the line only has one word
# spaces = [' '] * positionNum + [' ' * (maxWidth - currWidth)]
# result.append(''.join([s for pair in zip(oneLine, spaces) for s in pair]))
# return result
if __name__ == '__main__':
s = Solution()
print s.fullJustify(["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."],30)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.