LeetCode:Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
1 class Solution { 2 public: 3 void reverseWords(string &s) 4 { 5 //从前往后扫描 6 string res, word; 7 for(int i = s.size()-1; i >= 0;) 8 { 9 while(i >= 0 && s[i] == ' ')--i;//去掉空格 10 if(i < 0)break; 11 if(res.size() != 0)res.push_back(' '); 12 word.clear(); 13 while(i >= 0 && s[i] != ' ')word.push_back(s[i--]);//word为找到的一个单词 14 for(int j = word.size()-1; j >= 0; --j) 15 res.push_back(word[j]); 16 } 17 s = res; 18 } 19 };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3986615.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端