151. Reverse Words in a String (String)
思路: 本题考查的目的并不是使用字符串的函数。方法是两次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)的更多相关文章
- 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 ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- 关于 Dictionary<string,string>,和List<T>在View的使用
在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...
- alibaba fastjson List<Map<String, String>>2Str
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...
- The constructor User.Student(String, String, String) is not visible
项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...
- 从为什么String=String谈到StringBuilder和StringBuffer
前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...
- 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)
1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...
- ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”
ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...
随机推荐
- oracle登陆认证方式
转自:http://blog.itpub.net/14359/viewspace-683064/ 案例: 1,发现此时操作系统认证不成功: C:\Users\Administrator.WIN-201 ...
- beego注解路由 [自定义方法]
背景: beego生成的controller里面,默认get请求到由Get()方法处理:post请求由Post()方法处理 etc. 如果想自定义方法来处理请求,改怎么做? 直接拿beego的文档来说 ...
- HTML 圆心节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- strcore.cpp(156) 内存泄漏
vs搞了一个小工具涉及到线程 每次执行完退出的时候都会报 strcore.cpp(156) 的内存泄漏 原因是在线程内使用了CString 类型的传递参数 如果没有正常释放会报上面的错误.
- mongo aggregate 用法记录
mongo 聚合查询查询还是很方便的,做下记录 依赖的jar是org.springframework.data.mongodb 1.9.6 低版本可能不支持. 数据结构 大概是 这是一份 ...
- Delphi访问网页中的下拉菜单
Delphi通过TWebBrowser浏览网页,然后访问该网页中的下拉菜单: uses MsHtml;procedure TForm1.Button1Click(Sender: TObject);va ...
- matt cutts : try something new for 30 days
30 天尝试新事物matt cutts : try something new for 30 days[小计划帮你实现大目标] 是否有些事情, 你一直想去做, 但就是没有实现?马特 ?卡茨建议: 尝试 ...
- 【385】itertools 的 product 和 chain 和 accumulate
参考:itertools模块 product 相当于返回两个集合中数据的所有组合可能 Examples from Eric Martin from itertools import product p ...
- impdp导入文件失败问题解决(ORA-39001/ORA-39000/ORA-39143)
测试环境 SuSE11 + ORACLE11gR2 问题现象 执行 impdp导入现场导回的dmp文件,导入失败.错误提示如下 $impdp sysdb/oracle directory=imp_da ...
- Hibernate学习笔记2.1(Hibernate基础配置)
Hibernate基础配置 1.<property name="hbm2ddl.auto">update</property> 在SessionFactor ...