7. Reverse Words in a String
题目:
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.
解题思路: 先整体翻转,在把单词一个个翻转。(或反过来也行)
代码也许还能优化:
- class Solution {
- public:
- void reverseWords(string &s) {
- if(s == "") return;
- if(s.length() == popBlankLead(s)) return;
- else if(popBlankTrail(s) < 0) return;
- int judge = 0;
- while(judge < s.length() && s[judge] != ' ') ++judge;
- if(judge == s.length()) return;
- reverseAlpha(s, 0, s.length() - 1);
- int start = 0;
- for(int end = 0; end < s.length(); ++end){
- if(s[end] == ' '){
- while(s[end + 1] == ' '){
- s.erase(end, 1);
- }
- reverseAlpha(s, start, end - 1);
- start = end + 1;
- }
- }
- reverseAlpha(s, start, s.length() - 1);
- }
- void reverseAlpha(string &s, int begin, int end){
- if(s == "" || begin >= end) return;
- int mid = (end + begin +1) >> 1;
- for(int i = begin; i < mid; ++i){
- char c = s[i];
- s[i] = s[end];
- s[end--] = c;
- }
- }
- int popBlankLead(string &s){
- int first = 0;
- while(s[first] == ' ' && first < s.length()) ++first;
- s.erase(0, first);
- return first;
- }
- int popBlankTrail(string &s){
- int end = s.length() - 1;
- while(s[end] == ' ' && end >= 0) --end;
- s.erase(end + 1,s.length() - end - 1);
- return end;
- }
- };
精简版代码:
- class Solution {
- public:
- void reverseWords(string &s) {
- string buf;
- stringstream ss(s);
- vector<string> tokens;
- while (ss >> buf) tokens.push_back(buf);
- if (tokens.size() == 0) s="";
- else{
- int n = tokens.size()-1;
- s = tokens[n];
- for (int i = n-1; i >=0; -- i) s+=" "+tokens[i];
- }
- }
- };
7. Reverse Words in a String的更多相关文章
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- leetcode6 Reverse Words in a String 单词取反
Reverse Words in a String 单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 【LeetCode练习题】Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
随机推荐
- 1238. Folding
http://acm.timus.ru/problem.aspx?space=1&num=1238 DP+记忆化搜索 思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最 ...
- 验证页面多个input文本的必填项
前台页面 JS : function CheckMustWrite(){ var count = $("input[mustwrite = 'true']", document.f ...
- Win7下通过eclipse远程连接CDH集群来执行相应的程序以及错误说明
最近尝试这用用eclipse连接CDH的集群,由于之前尝试过很多次都没连上,有一次发现Cloudera Manager是将连接的端口修改了,所以才导致连接不上CDH的集群,之前Apache hadoo ...
- Python time模块学习
Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...
- HDU 4717 The Moving Points (三分法)
题意:给n个点的坐标的移动方向及速度,问在之后的时间的所有点的最大距离的最小值是多少. 思路:三分.两点距离是下凹函数,它们的max也是下凹函数.可以三分. #include<iostream& ...
- (转)初探Backbone
(转)http://www.cnblogs.com/yexiaochai/archive/2013/07/27/3219402.html 初探Backbone 前言 Backbone简介 模型 模型和 ...
- 命令参数解析库JCommonder
1.JCommander 是一个非常小的Java 类库,用来解析命令行参数. 2.参数类型:可以是任意类型,但我使用的只有 List,String. @Parameter(name="-s& ...
- LeetCode189——Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Certificate、Provisioning Profile、App ID
关于 Certificate.Provisioning Profile.App ID 的介绍及其之间的关系 2014-03-13 15:26 13416人阅读 评论(1) 收藏 举报 目录(?)[ ...
- C#—WebService
一.qq是否在线 1.添加Web引用 qqOnlineWebService cn.com.webxml.www.qqOnlineWebService shelly1 = new NIIT1109 ...