今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题。

题目描述:

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) {
string temp;
stack<string> sta;
for(int i=;i<s.length();i++)
{
if(s[i]!=' ')
{
temp+=s[i];
}
else
{
if(temp!="")
sta.push(temp);
temp="";
}
}
if(temp!="")
sta.push(temp);
s="";
while(sta.size()!=)
{
s+=sta.top();
sta.pop();
s+=' ';
}
s+=sta.top();
}
};

不得不说看起来很是正确啊自己也测试了一下各种空格的串,不管是前面后面都正确,但是交上去发现直接RE了。不过还好有提示(这个也是很向面试看齐的,感觉就是我给面试官那份代码,然后面试官问我,要是我输入的是空串你能正确输出吗?)这时候才发现我代码的不足于是乎赶紧加个判断,然后又想到全是空格的串会不会有问题?果然又发现了一个错误。。。再三检查后提交AC:

 class Solution {
public:
void reverseWords(string &s) {
if(s=="")
return ;
string temp;
stack<string> sta;
for(int i=;i<s.length();i++)
{
if(s[i]!=' ')
{
temp+=s[i];
}
else
{
if(temp!="")
sta.push(temp);
temp="";
}
}
if(temp!="")
sta.push(temp);
s=""; while(sta.size()>)
{
s+=sta.top();
sta.pop();
s+=' ';
}
if(sta.empty())
{
s="";
}
else
s+=sta.top();
}
};

虽说这是一道不难的题目,但是也很好的训练了思维的严密性,还是很有帮助的。但是个人感觉leetcode的这种模式以及题目资源比较国内主要面向竞赛的oj来说还是很有优势的,也推荐各位找工作的博友可以一起刷刷题,巩固一下基础。

------------------------------------------update 20141122-----------------------------------------------------

python版,一行代码解决

 class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
return " ".join(s.split()[::-1]) def main():
s = Solution()
print s.reverseWords("the sky is blue") if __name__ == '__main__':
main()

【leetcode】Reverse Words in a String的更多相关文章

  1. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  2. 【leetcode】Reverse Words in a String(hard)☆

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  3. 【leetcode80】Reverse Vowels of a String(元音字母倒叙)

    题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a ...

  4. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  5. 【字符串】Reverse Words in a String(两个栈)

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

  6. 【LeetCode】Reverse digits of an integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  7. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

  8. 【leetcode】Reverse Nodes in k-Group

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  9. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

随机推荐

  1. 如何优雅的使用 phpStorm 开发工具

    按照惯例依然是从百科上复制一条简介: PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具.PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化d ...

  2. SVN里常见的图标及其含义

    - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽略文件.A file ignored by version con ...

  3. meteor 为基础,联合 Apollo + React + React-Router

    Graphql with Apollo, Meteor and React: https://janikvonrotz.ch/2016/10/09/graphql-with-apollo-meteor ...

  4. hnu10104

    AC自动机+DFS #include <cstdio> #include <queue> #include <cstring> using namespace st ...

  5. 渲染物体到一张UITexture上

    把这个脚本挂到一个Camera上 using UnityEngine; using System.Collections; [RequireComponent(typeof(Camera))] pub ...

  6. mysql查询当前正在使用数据库

    1.select database(): 2.status: 3.show tables:

  7. 基于Maven构建开发第一个Storm项目

    前面说过了Storm的测试项目,那么此时我们更想自己写一个小项目来练练手,首先我们自己的Windows系统上首先应该安装好maven,然后启动Eclipse for JavaEE版本,接下来开始建立项 ...

  8. [NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UIButton* getButton; ...

  9. Struts2应用流程注解

    当Web容器收到请求(HttpServletReques   t)它将请求传递给一个标准的的过滤链包括(ActionContextCleanUp)过滤器. 经过Other filters(SiteMe ...

  10. 生成Geometry

    // 由一组点集生成一张三角面片网格Geometry osg::Geometry* createTRIANGLESGeometry(MyMesh &mesh) { osg::ref_ptr&l ...