【leetcode刷题笔记】Reverse Words in a String
2024-09-08 03:24:52
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
题解:用栈就行了
代码:
class Solution {
public:
void reverseWords(string &s) {
stack<string>st;
string temp = ""; for(int i = ;i <= s.length();i++){
if(i == s.length())
{
st.push(temp);
}
else if(s[i] != ' '){
temp += s[i];
}
else
{
if(temp != ""){
st.push(temp);
temp = "";
}
}
}
s = "";
while(!st.empty()){
if(st.top() != " ")
s += st.top();
st.pop();
if(!st.empty() && s != ""){
s += " ";
}
}
}
};
最新文章
- [make]makefile使用积累
- 新华龙电子推出最新网络开发板(W5100&;W5500方案)
- Android Handler机制(三)----Looper源码解析
- java中关于try、catch、finally中的细节分析
- Day20_IO第二天
- 基于Java的数据采集(终结篇)
- spring 注解简单使用
- sqlserver compact sdf, sqlite 数据库 在net中相对路径设置方法 - 摘自网络
- Sublime Text 3配置LiveReload实现实时刷新
- poj 1036 Gangsters
- Eclipse选项卡式的属性视图(The Eclipse Tabbed Properties View)
- 团队作业2:需求分析&;原型设计
- SAP MM ME29N 试图取消审批报错 - Document has already been outputed(function not possible) -
- openstack搭建之-基础服务配置(7)
- Mac 端配置 Lua 环境
- VC++、Win32 SDK、MFC的区别
- java.sql.SQLException: com.mysql.jdbc.Driver
- 【php】thinkphp以post方式查询时分页失效的解决方法
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
- BI开发(ETL-DW)
热门文章
- (六)Thymeleaf的 th:* 属性之—— th: ->;text&; utext&; href
- [腾讯 TMQ] 零基础学习 Fiddler 抓包改包
- index+small+row+if经典函数组合应用
- Java 下载JDK账号
- Git(四):理解和使用分支
- Android设计模式(十五)--备忘录模式
- 使用strace,lstrace,truss来跟踪程序的运行过程
- 图像处理之基础---2个YUV视频 拼接技术
- VS使用WEB DEPLOY发布
- output的使用