思路: 本题考查的目的并不是使用字符串的函数。方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转。

需要注意的是去除extra space,并且对全space字符串、以及最后一个单词要做特殊处理。

class Solution {
public:
void reverseWords(string &s) {
int start = ;
int end=-;
int i = ;
int cur = ; // point to the string with extra space deleted //ignore space at the beginning
while(i < s.length() && s[i]==' '){
i++;
} for(;i<s.length();i++){
if(s[i]==' ' && i+ < s.length() && s[i+] ==' ') continue; //ignore extra space between words
if(s[i]==' ' && i+ == s.length()) break; //ignore final space
s[cur++] = s[i];
if(s[i]==' '){
end = cur-; //end case1: the letter before space
reverse(s, start, end);
start = cur;
}
}
end = cur-; //end case2: the last not space letter!!!
if(end == -) s = ""; //special case: null string!!!
if(s[i-] != ' '){ //reverse the last word!!!
reverse(s,start,end);
}
cout << "end=" << end << endl;
s = s.substr(, end+);
reverse(s,,end);
} void reverse(string &s, int start, int end){
int l = start;
int r = end;
char tmp;
while(l<r){
tmp = s[l];
s[l]=s[r];
s[r]=tmp;
l++;
r--;
}
}
};

151. Reverse Words in a String (String)的更多相关文章

  1. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

  2. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  3. 入门:Java Map<String,String>遍历及修改

    重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...

  4. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

  5. alibaba fastjson List<Map<String, String>>2Str

    import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...

  6. getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:

    直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...

  7. The constructor User.Student(String, String, String) is not visible

    项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...

  8. 从为什么String=String谈到StringBuilder和StringBuffer

    前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...

  9. 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)

    1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...

  10. ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”

    ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...

随机推荐

  1. springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系

    结论: a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同. b.当使用@ModelAttribute注解请求参数时,springmvc自动将该 ...

  2. Asp.Net前台调用后台变量

    1.Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %&g ...

  3. Django--URL(路由层)

    一.django 静态文件配置 在配置文件中settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR ...

  4. google event

    一目了然,也不用多说了,随便记录下,内部实现基于观察者模式 TestEvent public class TestEvent { private final int message; public T ...

  5. mycat 分库分表

    单库分表已经在上篇写过了,这次写个分库分表,不同在于配置文件上的一点点不同 <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...

  6. mycat的schema.xml的个人的一点理解

    官方文档里讲的详细的部分的我就不再赘述了,我只是谈谈我自己的理解 刚开始接触mycat,最重要的几个配置文件有server.xml,schema.xml,还有个rule.xml配置文件 具体都是干啥用 ...

  7. fopen函数出现段错误

    昨天写代码的时候突然发现了一个问题,当使用fopen("<filepath>", "r")时,如果filepath不存在,那么fopen函数并不是像 ...

  8. [cocos2d-x]移动平台游戏开发(图)

    FreeMind的.mm文件下载: http://yunpan.cn/cfL3QrrQVkVTd (提取码:a125)

  9. vscode 右击文件||文件夹添加快捷方式

    操作注册表步骤 1.按下win+R 2.输入redegit,打开注册表 3.找到HKEY_CLASSES_ROOT/*/shell路径 4.新建/项:命名Open with visual code 5 ...

  10. 使用__all__限制模块可被导入对象

    经常我们会编写自定义模块,用于被别的脚本调用;有时候为了方便,会使用from module_name import *的方式导入,这样会把模块中全部对象导入进来; 使用__all__结合列表,可以控制 ...