Java C++算法題解leetcode1592重新排列單詞間的空格
更新時(shí)間:2022年09月14日 10:28:13 作者:AnjaVon
這篇文章主要為大家介紹了Java C++算法題解leetcode1592重新排列單詞間的空格示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
題目要求


思路:模擬
- 模擬就完了
- 統(tǒng)計(jì)空格數(shù)量和單詞數(shù)量,計(jì)算單詞間應(yīng)有的空格數(shù),將它們依次放入結(jié)果字符串,若有余數(shù)則在末尾進(jìn)行填補(bǔ)。
Java
class Solution {
public String reorderSpaces(String text) {
int n = text.length(), spcnt = 0;
List<String> words = new ArrayList<>();
for (int i = 0; i < n; ) {
if (text.charAt(i) == ' ' && ++i >= 0 && ++spcnt >= 0)
continue;
int j = i;
while (j < n && text.charAt(j) != ' ')
j++;
words.add(text.substring(i, j)); // 單詞
i = j;
}
StringBuilder res = new StringBuilder();
int m = words.size(), dis = spcnt / Math.max(m - 1, 1);
String spcs = ""; // 兩單詞間的空格
while (dis-- > 0)
spcs += " ";
for (int i = 0; i < m; i++) {
res.append(words.get(i));
if (i != m - 1)
res.append(spcs);
}
while (res.length() != n)
res.append(" ");
return res.toString();
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n),結(jié)果的空間開銷
C++
class Solution {
public:
string reorderSpaces(string text) {
int n = text.size(), spcnt = 0;
vector<string> words;
for (int i = 0; i < n; ) {
if (text[i] == ' ' && ++i >= 0 && ++spcnt >= 0)
continue;
int j = i;
while (j < n && text[j] != ' ')
j++;
words.emplace_back(text.substr(i, j - i)); // 單詞
i = j;
}
string res;
int m = words.size(), dis = spcnt / max(m - 1, 1);
string spcs = ""; // 兩單詞之間的空格
while (dis-- > 0)
spcs += " ";
for (int i = 0; i < m; i++) {
res += words[i];
if (i != m - 1)
res += spcs;
}
while (res.size() != n)
res += " ";
return res;
}
};
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n),結(jié)果的空間開銷
Rust
- rust有很方便的函數(shù)用以統(tǒng)計(jì)空格和單詞,也有很方便的
repeat構(gòu)成單詞之間需要的空格。
impl Solution {
pub fn reorder_spaces(text: String) -> String {
let spcnt = text.chars().filter(|&c| c == ' ').count();
let words: Vec<String> = text.split_whitespace().map(|s| s.to_string()).collect();
let mut res = String::new();
if words.len() == 1 {
res.push_str(&words[0]);
res.push_str(&" ".repeat(spcnt));
return res
}
for i in 0..words.len() {
res.push_str(&words[i]);
res.push_str(&" ".repeat(
if i < words.len() - 1 {
spcnt / (words.len() - 1)
}
else {
spcnt - spcnt / (words.len() - 1) * (words.len() - 1)
}));
}
res
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n),結(jié)果的空間開銷
以上就是Java C++算法題解leetcode1592重新排列單詞間的空格的詳細(xì)內(nèi)容,更多關(guān)于Java C++ 單詞間空格重排的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- C C++算法題解LeetCode1408數(shù)組中的字符串匹配
- Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)
- Java?C++題解leetcode字符串輪轉(zhuǎn)KMP算法詳解
- Java C++ 算法題解leetcode1582二進(jìn)制矩陣特殊位置
- Java?C++?算法題解leetcode145商品折扣后最終價(jià)格單調(diào)棧
- Java C++ 算法leetcode828統(tǒng)計(jì)子串中唯一字符乘法原理
- Java?C++?算法題解leetcode669修剪二叉搜索樹示例
- c++算法進(jìn)階刪除有序鏈表中的重復(fù)元素
相關(guān)文章
java 出現(xiàn)NullPointerException的原因及解決辦法
這篇文章主要介紹了java 出現(xiàn)NullPointerException的原因及解決辦法的相關(guān)資料,這里說明出現(xiàn)NullPointerException 的原因的總結(jié),并說明該如何解決,需要的朋友可以參考下2017-08-08
C++實(shí)現(xiàn)LeetCode(206.倒置鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(206.倒置鏈表),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)圖書管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

