【leetcode】Reverse Words in a String
今天第一次在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的更多相关文章
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 【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& ...
- 【leetcode80】Reverse Vowels of a String(元音字母倒叙)
题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【字符串】Reverse Words in a String(两个栈)
题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...
- 【LeetCode】Reverse digits of an integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)
这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...
- 【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 ...
- 【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 ...
随机推荐
- Resizing the disk space on Ubuntu Server VMs running on VMware ESXi 5
from: http://www.joomlaworks.net/blog/item/168-resizing-the-disk-space-on-ubuntu-server-vms-running- ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
- MyISAM 调度(优先级)的一些优化【转】
MySQL的MyISAM引擎现在越来越被淡化了,但是还是有必要再温习总结一下的. 允许你改变语句调度的优先级,它可以使来自多个客户端的查询更好地协作,这样单个客户端就不会由于锁定而等待很长时间.改变优 ...
- Effective C++ -----条款39:明智而审慎地使用private继承
Private继承意味is-implemented-in-terms of(根据某物实现出).它通常比复合(composition)的级别低.但是当derived class需要访问protected ...
- (转)JAVA AJAX教程第二章-JAVASCRIPT基础知识
开篇:JAVASCRIPT是AJAX技术中不可或缺的一部分,所以想学好AJAX以及现在流行的AJAX框架,学好JAVASCRIPT是最重要的.这章我给大家整理了一些JAVASCRIPT的基础知识.常用 ...
- mybatis 获取自增ID
在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <inser ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【processing】小代码5
3D void setup() { size(,,P3D); } void draw() { background(); lights(); noStroke(); translate(,,-); r ...
- python2.7之MySQLdb模块 for linux安装
1.下载:MySQL-pythonhttp://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3b1/MySQL- ...