leetcode系列(1)Reverse Words in a String 句中单词顺序翻转
来源:互联网 发布:网络推广员怎么做 编辑:程序博客网 时间:2024/05/16 15:49
题目描述如下
Reverse Words in a String Total Accepted: 5802 Total Submissions: 41330
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
咋一看就是《编程珠玑》里面的一个经典的例子,思路就是先翻转句子中每个单词,然后翻转整个句子;但是为什么ac率这么低呢,我也没有注意,直接上代码sub,被毫不留情的挂了,提示如果输入“ ”,应该输出“”,这不科学啊,我看了下classification,发现描述如下:
Clarification:
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
原来是问题中还隐藏了一个字符串中的空格处理问题啊,也不怪众屌丝,因为描述中没有说清楚要去除前导和拖尾的空格以及中间的多个连续空格变一个,不过无所谓了,加一个预处理函数就行了,自己先在xcode上跑了几个测试用例,然后粘贴sub直接ac了,代码如下://// reverse_word_string.cpp// leet_code//// Created by meng duan on 14-3-29.// Copyright (c) 2014年 886524. All rights reserved.// Given an input string, reverse the string word by word.// For example,// Given s = "the sky is blue",// return "blue is sky the"./* Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input string contain leading or trailing spaces? Yes. However, your reversed string should not contain leading or trailing spaces. How about multiple spaces between two words? Reduce them to a single space in the reversed string. */#include "common.h"class SolutionReverseWordString {public: typedef string::iterator ite_str_t; void pre_process (string &str) { string rst = ""; bool flag = true; // remove all the pre space ite_str_t ite; for (ite = str.begin(); ite != str.end(); ++ite) { if (' ' != (*ite)) break; } if (ite == str.end()) { str = rst; return; } ite_str_t last; for (last = str.end(); last != str.begin(); --last) { if (*(last-1) != ' ') break; } for ( ; ite != last; ++ite) { if (' ' != *ite) { rst.push_back(*ite); flag = true; } if (flag && (*ite == ' ')) { rst.push_back(*ite); flag = false; } } str = rst; } void reverse_word (ite_str_t start, ite_str_t end) { while (start < end) { char tmp = *start; *start = *end; *end = tmp; ++start; --end; } return; } void reverse_word_string (string &str) { pre_process(str); // ignore the pre space char in the string ite_str_t ite = str.begin(); for ( ; ite != str.end(); ++ite) { if (' ' != (*ite)) break; } // if all char is space just return if (str.end() == ite) return; ite_str_t s = ite; ite_str_t e = ite + 1; while (str.end() != s) { while (' ' == (*s)) ++s; if (str.end() == s) break; while ((' ' != (*e)) && (e != str.end())) e++; reverse_word(s, e-1); s = e; ++e; } reverse_word(str.begin(),str.end()-1); }};int main(int argc, char **argv) { SolutionReverseWordString test_obj; string t1 = " "; string t2 = "a "; string t3 = " b"; string t4 = "duan meng "; string t5 = " duan meng is macduan "; test_obj.reverse_word_string(t1); cout<<"t1 is : "<<t1<<endl; test_obj.reverse_word_string(t2); cout<<"t2 is : "<<t2<<endl; test_obj.reverse_word_string(t3); cout<<"t3 is : "<<t3<<endl; test_obj.reverse_word_string(t4); cout<<"t4 is : "<<t4<<endl; test_obj.reverse_word_string(t5); cout<<"t4 is : "<<t5<<endl; return 0;}
0 0
- leetcode系列(1)Reverse Words in a String 句中单词顺序翻转
- 翻转单词顺序 Reverse Words in a String
- Reverse Words in a String 翻转单词顺序
- Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
- LeetCode OJ 之 Reverse Words in a String (翻转字符串的单词)
- 151. Reverse Words in a String & 186.Reverse Words in a String II & 翻转单词顺序列
- 将一个英文句子单词顺序逆置(LeetCode | Reverse Words in a String)
- LeetCode 151. Reverse Words in a String(反转单词)
- Reverse Words in a String 翻转句子的单词
- 151. Reverse Words in a String 翻转字符串中的单词
- Reverse Words in a String 反转单词顺序
- leetcode 151. Reverse Words in a String 字符串 翻转
- [LeetCode]Reverse Words in a String-反转单词
- [Leetcode] reverse words in a string 反转单词
- 【LeetCode】{Reverse Words in a String} 单词间只留一个空格
- LeetCode 1 Reverse Words in a String
- linux 下DNW 传送文件
- 黑马程序员_Java泛型
- struct中出现id to load is required for loading的错误
- Xcode 5 快捷键
- 二分
- leetcode系列(1)Reverse Words in a String 句中单词顺序翻转
- 练习monkeyrunner(1)
- thinking in java中出现的设计模式代码例子
- 关于git
- 设计模式 之“他见”与“我解”
- 转换流
- android 绘制柱状图等
- 练习monkeyrunner(2)
- Cookie小结