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 += " ";
}
}
}
};

【leetcode刷题笔记】Reverse Words in a String的更多相关文章

  1. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  2. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  5. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  6. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  7. 【leetcode刷题笔记】Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. 【leetcode刷题笔记】Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

随机推荐

  1. PHP面试题及答案解析(3)—MySQL数据库

    1.mysql_num_rows()和mysql_affected_rows()的区别. mysql_num_rows()和mysql_affected_rows(),这两个函数都作用于 mysql_ ...

  2. POJ1328 Radar Installation 【贪心&#183;区间选点】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12 ...

  3. ganlia安装配置文档

    gangliaz在ubuntu中安装和配置很简单 1.  服务器端安装 sudo apt-get install ganglia-monitor ganglia-webfrontend rrdtool ...

  4. wxWidgets之wxGrid控件

    1. 介绍 wxGrid控件时wxWidgets界面库中内置的网格控件. 通经常使用来显示表格数据.该控件拥有强大的功能.开发人员可依据自己的需求对其进行定制. 2. 经常使用API      构造函 ...

  5. POI 实现合并单元格以及列自适应宽度

    POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...

  6. Android Activity去除标题栏和状态栏

    一.在代码中设置public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去除ti ...

  7. 定时器:Timer:System.Threading.Timer类(转)

    最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...

  8. junit spring 测试

    http://my.oschina.net/dlpinghailinfeng/blog/336694 http://blog.csdn.net/zhangzikui/article/details/1 ...

  9. MUI 清除缓存

    mui 清除但是在ios和安卓稍微有点区别, ios可以清除的很彻底,下载文件也能删除: 安卓能清理缓存,但是不能删除下载的文件: plus.cache.calculate(function(size ...

  10. hdu4847:Wow! Such Doge!(字符串匹配)

    题目:hdu4847:Wow! Such Doge! 题目大意:在给出的段落里面找出"doge"出现的次数.大写和小写都能够. 解题思路:字符串匹配问题,能够在之前将字母都转换成统 ...