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 ...
随机推荐
- ThinkPHP同时操作多个数据库
除了在预先定义数据库连接和实例化的时候指定数据库连接外,我们还可以在模型操作过程中动态的切换数据库,支持切换到相同和不同的数据库类型.用法很简单, 只需要调用Model类的db方法,用法: $this ...
- node.js 发送邮件
var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport'); // ...
- 【JEECG技术文档】Jeecg高级查询器
1. 背景 对于用户来讲查询功能按易用性分三个层次: 1)最简单查询操作是一个输入框,全文检索,如百度,后台实现技术使用搜索引擎,需要设计和建立索引,技术较为复杂,适用于文档和信息数据库检索,但是结果 ...
- jquery左侧菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Delphi 修改本地日期和时间
procedure TForm1.ModifySysdate(D: Double); var systemtime:Tsystemtime; DateTime:TDateTime; begin Set ...
- SITE STRUCTURE
SITE STRUCTURE HTML Review Congratulations! You've learned enough HTML to create a great website! Be ...
- linux下mysql开启远程访问权限 防火墙开放3306端口
linux下mysql开启远程访问权限 防火墙开放3306端口 转载 2017-01-21 作者:JAVA-ANDROID 这篇文章主要为大家详细介绍了linux下mysql开启远程访问权限,防 ...
- APP安全性测试总结--网上转载
移动APP安全测试 老鹰a0人评论7103人阅读2018-08-06 16:22:07 1 移动APP安全风险分析 1.1 安全威胁分析 安全威胁从三个不同环节进行划分, ...
- 软件工程github使用小结
1.在 https://github.com/join 这个网址处申请注册一个Github账号,申请成功后可在https://github.com/login 处利用刚刚注册的账号进行登录,才能开始在 ...
- MySQL缓存分类和配置
读书笔记,待补充完善 MySQL缓存分类 InnoDB缓冲池 InnoDB日志文件和MyIsAM数据的操作系统缓存 MyIsAM键缓存 查询缓存 无法手工配置的缓存,二进制日志,表定义文件的操作系统缓 ...