Leetcode#151 Reverse Words in a String
将单词按空格分词,然后倒序拼接即可
代码:
void reverseWords(string &s) {
vector<string> words;
int start = -;
int len = ;
for (int i = ; i < s.length(); i++) {
if (s[i] == ' ') {
if (len > )
words.push_back(s.substr(start, len));
len = ;
}
else {
if (len == ) {
start = i;
len = ;
}
else
len++;
}
}
if (len > )
words.push_back(s.substr(start, len));
string res;
if (words.size() > ) {
for (int i = words.size() - ; i > ; i--)
res += words[i] + " ";
res += words[];
}
s = res;
}
如果不使用额外的辅助空间,可以用递归,将原问题转化成颠倒第一个单词和剩下的单词,而剩下的单词是一个子问题,于是就这么做下去就可以了。代码待补充。原先做这道题的时候没有想到用这种方法,后来在面试Juniper的时候面试官问道了,当时我没想到,但是经过提示我才想到了这个方法,汗。
Leetcode#151 Reverse Words in a String的更多相关文章
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Java for LeetCode 151 Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- leetcode 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- LeetCode 151 reverse word in a string
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 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 ...
- 【刷题-LeetCode】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
随机推荐
- Maven的HTTP代理设置
http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html 第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...
- win7防火墙打不开(无法启动windows firewall服务)
点击windows 7控制面板中防火墙的“推荐配置”没有反应:打开“服务”,无法启动windows firewall,并报错. 可能很多的win7用户都碰到过这样的一种情况,那就是win7的防火墙打 ...
- mvc url路由参数的加密和解密
查看某个信息的时候一般会在url上加上该信息在数据库中对应的主键id(而且一般是自增的) url是这样子的 xxxDetail/1 , 虽然对于我们开发人员来说可以这种显式的数据库主键会方便调试过程, ...
- gRPC Client的负载均衡器
一.gRPC是什么? gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协 ...
- IOS应用程序生命周期
一.IOS应用的5种状态 Not Running(非运行状态) 应用没有运行或被系统终止. Inactive(前台非活动状态) 应用正在进入前台状态,但是还不能接受事件处理. Active(前台活动状 ...
- 中兴软件编程规范C/C++
Q/ZX 深圳市中兴通讯股份有限公司企业标准 (设计技术标准) Q/ZX 04.302.1–2003 软件编程规范C/C++ 20 ...
- 第一步 django的下载安装
django是python众多web框架中比较有名的一个,以大包大揽功能俱全而著名.但作为重量级的web框架,难免性能上回有所损失,不过由于其封装了各种API,在开发的时候会便利许多.所以也是深受欢迎 ...
- Python学习教程(learning Python)--1.2.3 Python格式化输出百分比
在有些情况下,需要百分比输出数据,我们可以继续使用Python内建函数format来实现百分比的数据输出. >>> print(format(0.5236, '.2%')) 其结果如 ...
- App.config的学习笔记
昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config不大可能了. WP具体支持API请查 ...
- highcharts 显示点值的效果
plotOptions: { line: { /* <s:if test='#request.rdflags=="point"'> <s:if test=&quo ...