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: ...
随机推荐
- PHP字符串拼接与MySQL语句
这个部分总是钻牛角尖.总是出错. public function getList($pagesize=25){ $where = '1'; $tableName = $this->getTabl ...
- php循环创建目录
代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...
- SBM is Not Sale And Run Company
data = """ Well,We will bet you dollars to donuts,there are so many crusher provider ...
- andoroid项目使用Javah找不到class问题
比如目录结构是:Soffice\bin\classes\cn\com\isoffice\util\SofficeWebService.class 进入到bin/classes 下使用命令 javah ...
- JQuery遍历指定id的div name值的几种方法
JQuery遍历指定id的div name值的几种方法:方法一 $("#div1 :text").each(function () { var this_id = $(this). ...
- Moses与IRSTLM共同编译失败的解决方案:fatal error: dictionary.h no such file or 目录
已经解决: 错误原因在于始终没用又用已经编译安装过的irstlm而是一直用那个原文件夹造成的,而这里Manual似乎也写错了,manual里有很强的误导性:
- Microsoft Azure 的一些限制 Global
Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdu 1496 Equations
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...
- spring使用JdbcDaoSupport中封装的JdbcTemplate进行query
1.Dept package cn.hxex.springcore.jdbc; public class Dept { private Integer deptNo; private String d ...