[leetcode-557-Reverse Words in a String III]
Given a string, you need to reverse the order of characters in each word within a sentence while
still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
思路:
用一个vector<vector<char> >收集所有的字符,空格为单词分割符,将每一个单词翻转后,输出即可。
感觉思路还比较朴素,但是看上去很啰嗦。
string reverseWords(string s)
{
if(s == "")return s;
vector<vector<char> >sentence;
vector<char>word;
int i=;
while(s[i] != '\0')
{
if(s[i]!=' ')
{
word.insert(word.begin(),s[i]);
}
else
{
sentence.push_back(word);
word.clear();
}
i++;
}
sentence.push_back(word);
char res[];//太小 要用100000
int ind =;
for(int i=;i<sentence.size();i++)
{
for(int j=;j<sentence[i].size();j++)
{
res[ind] = sentence[i][j];
ind++;
}
res[ind] = ' ';
ind++;
}
res[ind-] = '\0';
return res;
}
[leetcode-557-Reverse Words in a String III]的更多相关文章
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 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] 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 (C++) stringstream
1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...
- 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 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
随机推荐
- 域名系统DNS和FTP
域名系统概述 域名系统DNS(Domain Name System)是英特网使用的命名系统,用于把便于人们使用机器名字转化为IP地址. 为什么机器在处理IP数据报时要使用IP地址而不使用域名呢?IP地 ...
- 关于微信小程序的Request请求错误处理
在学微信小程序的request请求的时候,一开始报“不在以下合法域名列表中,请参考文”的错误,后来又莫名其妙的报“400 Bad Request”错误,经过半天的研究,终于搞定了,把遇到的错误给大家分 ...
- OOP 三大特点:继承性,封装性,多态性
1.继承性:代码重用 2.封装性: 使相似数据和操作进行封装,保持代码安全 3.多态性: PHP不支持多态
- 在centos7下安装python3
环境搭建 准备工具: centos7:http://mirror.bit.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1611.iso virtus ...
- 每天一道Java题[5]
题目 String.StringBuilder.StringBuffer有什么异同? 解答 相同点:String.StringBuilder.StringBuffer都可以用来存储字符串. 不同点: ...
- 连锁反应confirm
<script> function del(){ var flag = confirm("你真要删除么?"); if( flag ){ alert("我已被你 ...
- Zookeer-- 上搭建 hbase
一.HBase的相关概念1.HBase的概念:大量数据进行随机近实时读写时使用Hbase.2.HBase是一个模仿Gootable's Bigtable的,开源的.分布式的.版本化的非关系型数据库.3 ...
- Day1-三元运算及
三元运算:result = Value1 if Condition else Vlaue2 >>> a,b,c = 1,3,5>>> d = a if a > ...
- 浅谈 Java 主流开源类库解析 XML
在大型项目编码推进中,涉及到 XML 解析问题时,大多数程序员都不太会选用底层的解析方式直接编码. 主要存在编码复杂性.难扩展.难复用....,但如果你是 super 程序员或是一个人的项目,也不妨一 ...
- Mac下修改环境变量并保存
1. 终端执行 touch ~/.bash_profile 2.终端执行 vim ~/.bash_profile 3.点一下向下箭头,点E 进入bash_profile文件 4.点i进入编辑模式,进行 ...