Editor的信很重要吗?# Immigration - 落地生根
a*e
1 楼
我不知道这种情况怎么继续努力啦,只能看看别人的答案?我觉得自己的思路是对的啊,
。。。有人碰到类似情况吗?
Last executed input:
["My","momma","always","said,",""Life","was","like","a","box
","of","chocolates.","You","never","know","what","you're","gonna","get."],
12
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.
class Solution {
public:
vector fullJustify(vector &words, int L) {
int n = words.size();
vector res;
if (n == 0) return res;
string tmp = words[0];
int num = 0;//num of spaces in between words
int start = 0;
for (int i = 1; i {
while (tmp.length() + words[i].length() + 1 {
tmp = tmp + ' ' + words[i];
num++;
i++;
}
if (i >= n) break;
//add empty space
if (tmp.length() {
int morespace = L - tmp.length();
int perspace = num == 0 ? morespace : morespace / num;
int extraspace = num == 0 ? 0 : morespace%num;
tmp.insert(words[start].length(), perspace + extraspace, ' ');
int pos = words[start].length() + perspace + extraspace+1;
for (int j = start + 1; j{
pos += words[j].length()+1;
tmp.insert(pos, perspace, ' ');
}
}
res.push_back(tmp);
tmp = words[i];
num = 0;
start = i;
}
res.push_back(tmp);
return res;
}
};
。。。有人碰到类似情况吗?
Last executed input:
["My","momma","always","said,",""Life","was","like","a","box
","of","chocolates.","You","never","know","what","you're","gonna","get."],
12
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.
class Solution {
public:
vector
int n = words.size();
vector
if (n == 0) return res;
string tmp = words[0];
int num = 0;//num of spaces in between words
int start = 0;
for (int i = 1; i
while (tmp.length() + words[i].length() + 1
tmp = tmp + ' ' + words[i];
num++;
i++;
}
if (i >= n) break;
//add empty space
if (tmp.length()
int morespace = L - tmp.length();
int perspace = num == 0 ? morespace : morespace / num;
int extraspace = num == 0 ? 0 : morespace%num;
tmp.insert(words[start].length(), perspace + extraspace, ' ');
int pos = words[start].length() + perspace + extraspace+1;
for (int j = start + 1; j{
pos += words[j].length()+1;
tmp.insert(pos, perspace, ' ');
}
}
res.push_back(tmp);
tmp = words[i];
num = 0;
start = i;
}
res.push_back(tmp);
return res;
}
};