leetcode1451 - Rearrange Words in a Sentence - medium
Given a sentence text (A sentence is a string of space-separated words) in the following format:
- First letter is in upper case.
- Each word in
textare separated by a single space.
Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order.
Return the new text following the format shown above.
Example 1:
Input: text = "Leetcode is cool" Output: "Is cool leetcode" Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4. Output is ordered by length and the new first word starts with capital letter.
Example 2:
Input: text = "Keep calm and code on" Output: "On and keep calm code" Explanation: Output is ordered as follows: "On" 2 letters. "and" 3 letters. "keep" 4 letters in case of tie order by position in original text. "calm" 4 letters. "code" 4 letters.
Example 3:
Input: text = "To be or not to be" Output: "To be or to be not"
Constraints:
textbegins with a capital letter and then contains lowercase letters and single space between words.1 <= text.length <= 10^5
用map存len2[words], 因为key自动被sort好了,loop map的时候就自然先get短的string。
分割方法:遍历text,用一个tmp str来存当前word,遍历到空格的时候就是这个word组装完了,存map里,然后把tmp清空。注意为了把最后一个char也考虑进来,要遍历到它后一位,然后组装完的条件再加一个或者是i == text.size()的时候。这个过程也确保了相同长度的单词在结果里的相对顺序就是原来的顺序。
最后记得大写首字母,pop最后的空格。
用法:
tolower,toupper中间没有下划线,而且是有返回值的,c = tolower(c)这样用。
map排序是升序。
std::map<K,V> is ordered based on the key, K, using std::less<K> to compare objects, by default.map里面loop:first、second没括号没括号没括号, 也不是用[0]、[1]
for ( const auto &p : mp )
{
std::cout << p.first << '\t' << p.second << std::endl;
}
for ( auto it = mp.begin(); it != mp.end(); ++it )
{
std::cout << it->first << '\t' << it->second << std::endl;
}
实现:
class Solution {
public:
string arrangeWords(string text) {
string res;
map<int, vector<string>> mp;
text[0] = tolower(text[0]);
string word;
for (int i=0; i<=text.size(); i++){
if (text[i] == ' ' || i == text.size()){
mp[word.size()].push_back(word);
word.clear();
}else{
word += text[i];
}
}
for (auto &row : mp){
for (string word : row.second){
res += word + " ";
}
}
res[0] = toupper(res[0]);
res.pop_back();
return res;
}
};
法2:
Stable sort - sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort preserves the relative order of the elements with equivalent values.
class Solution { public: string arrangeWords(string text) { string res; stringstream ss(text); string tmp; vector<string> words; while (ss >> tmp) { words.push_back(tmp); } if (!words.empty()) words[0][0] = tolower(words[0][0]); stable_sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.size() < b.size(); }); for (auto& w : words) { res += w + " "; } if (!res.empty()) res[0] = toupper(res[0]); res.pop_back(); return res; } };
法3:
map<int, string> map里直接拼好相同长度的words,最后用stringstream拼好完整结果
class Solution { public: string arrangeWords(string text) { text[0] = tolower(text[0]); istringstream iss(text); ostringstream oss; string word; map<int, string> mp; while (iss >> word) mp[word.size()] += word+' '; for (auto& elem : mp) oss << elem.second; string res = oss.str(); res[0] = toupper(res[0]); res.pop_back(); return res; } };

浙公网安备 33010602011771号