今天第一次在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. JavaScript——exec和match

    题目17:Read the following javascript code: var someText="web2.0 .net2.0";var pattern=/(\w+)( ...

  2. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  3. 渲染物体到一张UITexture上

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

  4. MySQL 四种事务隔离级的说明

    很早之前写的文章,重新回顾和学习下: 按照SQL:1992 事务隔离级别,InnoDB默认是可重复读的(REPEATABLE READ).MySQL/InnoDB 提供SQL标准所描述的所有四个事务隔 ...

  5. C#小知识特殊的DefaultValueAttribute

    今天看别人写的源码发现一个特殊的DefaultValueAttribute.使用如下. 原始代码: public class People { private string _Name = " ...

  6. Effective C++ -----条款43:学习处理模板化基类内的名称

    可在derived class templates内通过“this->“指涉base class templates内的成员名称,或藉由一个明白写出的”base class资格修饰符”完成.

  7. HDU 4940 Destroy Transportation system(无源汇有上下界最大流)

    看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...

  8. 使用json格式输出

    /** * json输出 * * @param unknown_type $info */ public function json_out ($info) { header('Content-typ ...

  9. C++基础练习题(一): 查找最短单词

    /*<说明> 编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找 </说明>*/ #include<time.h> #inclu ...

  10. IOS- 自定义 UIButton

    #pragma mark init方法内部默认会调用initWithFrame: - (id)initWithFrame:(CGRect)frame { self = [super initWithF ...