File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } words
3
+ * @param {number } maxWidth
4
+ * @return {string[] }
5
+ */
6
+ const fullJustify = function ( words , maxWidth ) {
7
+ const res = [ ]
8
+ let curRow = [ ]
9
+ let numOfChars = 0
10
+
11
+ for ( let w of words ) {
12
+ if ( numOfChars + w . length + curRow . length > maxWidth ) {
13
+ for ( let i = 0 ; i < maxWidth - numOfChars ; i ++ ) {
14
+ if ( curRow . length === 1 ) {
15
+ curRow [ 0 ] += ' '
16
+ } else {
17
+ curRow [ i % ( curRow . length - 1 ) ] += ' '
18
+ }
19
+ }
20
+ res . push ( curRow . join ( '' ) )
21
+ curRow = [ ]
22
+ numOfChars = 0
23
+ }
24
+ curRow . push ( w )
25
+ numOfChars += w . length
26
+ }
27
+
28
+ const numOfSpace = maxWidth - numOfChars - ( curRow . length - 1 )
29
+ let tail = ''
30
+ for ( let i = 0 ; i < numOfSpace ; i ++ ) tail += ' '
31
+ res . push ( curRow . join ( ' ' ) + tail )
32
+
33
+ return res
34
+ } ;
35
+
36
+ // another
37
+
1
38
/**
2
39
* @param {string[] } words
3
40
* @param {number } L
You can’t perform that action at this time.
0 commit comments