</pre><h3 style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; margin-top: 20px; margin-bottom: 10px; font-size: 24px; color: rgb(51, 51, 51); display: inline;">Reverse Words in a String</h3><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> </span><p></p><p></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">Given an input string, reverse the string word by word.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">For example,<br style="" />Given s = "<code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); white-space:nowrap; background-color:rgb(249,242,244)">the sky is blue</code>",<br style="" />return "<code style="font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); white-space:nowrap; background-color:rgb(249,242,244)">blue is sky the</code>".</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><a target=_blank target="_blank" href="https://oj.leetcode.com/problems/reverse-words-in-a-string/#" style="color:rgb(0,136,204); text-decoration:none">click to show clarification.</a></p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">思路:</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">1、从尾部开始读取字符串</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">2、跳过字符串中的空格</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">3、逆序答应非空格的单词</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">空间复杂度:额外使用两个字符串</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">时间复杂度:O(n)</p><p></p><pre name="code" class="cpp">#include <string>
using std::string;
void reserveWords(string& s){
if(s.empty()){
return;
}
int index = s.length() - 1;
int c_size = 0;
string rs;
while (index >= 0){
c_size = 0;
while (index >= 0 && s[index] == ' '){
index --;
}
if (index < 0){
break;
} string t;
while(index >= 0 && s[index] != ' '){
t.push_back(s[index--]);
c_size++;
}
if(!rs.empty()){
rs.push_back(' ');
}
for(int i = c_size - 1; i >= 0; i--){
rs.push_back(t[i]);
}
}
printf(rs.c_str());
}
int main(){
string s = " ";
reserveWords(s);
return 0;
}
LeetCode: Reverse Words in a String
最新推荐文章于 2025-09-11 19:19:55 发布