LeetCode: Reverse Words in a String

</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;
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值