일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 715. Range Module
- data science
- 315. Count of Smaller Numbers After Self
- Class
- 43. Multiply Strings
- concurrency
- Python Code
- 시바견
- 파이썬
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 프로그래머스
- LeetCode
- 30. Substring with Concatenation of All Words
- Python
- 운영체제
- 컴퓨터의 구조
- kaggle
- shiba
- 밴픽
- Substring with Concatenation of All Words
- t1
- Generator
- Python Implementation
- attribute
- Regular Expression
- Protocol
- Decorator
- Convert Sorted List to Binary Search Tree
- iterator
Archives
- Today
- Total
Scribbling
LeetCode: 68. Text Justification 본문
Just a mere implementation problem.
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
ret = []
i = 0
while i <= len(words) - 1:
nowWords = [words[i]]
nowlen = len(words[i])
numchar = len(words[i])
i += 1
while i <= len(words) - 1 and nowlen + len(words[i]) + 1 <= maxWidth:
nowWords.append(words[i])
nowlen += len(words[i]) + 1
numchar += len(words[i])
i += 1
if len(nowWords) != 1 and i != len(words):
numblanks = maxWidth - numchar
quotient, remainder = numblanks // (len(nowWords)-1), numblanks % (len(nowWords)-1)
blanks = [quotient] * (len(nowWords)-1)
for j in range(remainder):
blanks[j] += 1
nowSentence = nowWords[0]
for j in range(1, len(nowWords)):
nowSentence += ' ' * blanks[j-1]
nowSentence += nowWords[j]
ret.append(nowSentence)
else:
nowSentence = nowWords[0]
for j in range(1, len(nowWords)):
nowSentence += ' '
nowSentence += nowWords[j]
nowSentence += ' ' * (maxWidth - nowlen)
ret.append(nowSentence)
return ret