【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 ...
随机推荐
- Navicat for MySQL的使用
一. 在Navicat for MySQL软件中,如何打开MySQL命令行界面: 图 (1) 如何调出MySQL命令行界面 如图(1)所示,在左侧空白处,右键单击即可调出“命令列介面” 注意,输入My ...
- HTML——选择器
在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元素选择器和属性选择器允许您通过标签名.属性 ...
- [转]Java连接各种数据库的方法
//MySQL: String Driver="com.mysql.jdbc.Driver"; //驱动程序 String URL="jdbc:m ...
- SQL 执行计划(一)
缓存执行计划 SQL Server 2008提供了一些服务器对象来分析执行计划Sys.dm_exec_cached_plans: 包含缓存的执行计划,每个执行计划对应一行.Sys.dm_exe ...
- (转载)使用 udev 高效、动态地管理 Linux 设备文件
概述: Linux 用户常常会很难鉴别同一类型的设备名,比如 eth0, eth1, sda, sdb 等等.通过观察这些设备的内核设备名称,用户通常能知道这些是什么类型的设备,但是不知道哪一个设备是 ...
- MySQL表中数据的迁移
INSERT INTO `crm_attachment`(OPERATOR_ID,ATTACHMENT_ID,TYPE ) SELECT APPLICATION_ID ,ATTACHMENT_ID,' ...
- IsNullOrEmpty与IsNullOrWhiteSpace区别
IsNullOrEmpty public static bool IsNullOrEmpty(String value) { return (value == null || value.Length ...
- 【STL】重载运算符
重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- #ifndef -摘自百度百科
#ifndef 标识1 //判断"标识1"是否定义,如果被定义则返回假,如果没有被定义则返回真. /**********************************/ 语句1 ...