[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 ...
随机推荐
- Oracle 12C 新特性之非分区表转分区表online clause(不停业务+索引有效)
12c以前非分区表需要转换为分区, 如果不停业务的话可以使用在线重定义,只有在表进行切换的时候会有短暂的锁表. 12c 中alter table online clause 实现了表上现有的索引有效, ...
- let与const详解
在ES6中,js首次引入了块级作用域的概念,而什么是块级作用域? 众所就知,在js当中存在预解析的概念,就是变量提升.并且只存在全局作用域和私有作用域.在全局定义的变量就是全局变量,而在函数内部定义的 ...
- 【WPF MaterialDesign 示例开源项目】 Work Time Manager
转岗写了将近一年的 PHP 最近因为 工作太多太杂, 在汇报工作的时候经常会忘记自己做了些什么,本来想只是使用excel来记录,但是发现了excel的很多局限性,光是无法共享就郁闷死了,习惯了下班不带 ...
- Flight学习(一)
翻看git时看到有新消息提示,点进去第一个就是Flight,那么今天就简单来翻译和了解一下这个框架吧,锻炼下自己的英文文档阅读能力,同时也熟悉下JavaScript.时间太赶,难免出现翻译失误和错误, ...
- 基于Groovy应用程序的spring boot
spring boot CLI 它是使用Spring Boot的最简单的和快速的的方法.他是一个基于Groovy脚本的命令工具.可以按照以下步骤安装次工具: 1.去spring官网下载 http:// ...
- DATA VISUALIZATION – PART 2
A Quick Overview of the ggplot2 Package in R While it will be important to focus on theory, I want t ...
- opencv基础到进阶(2)
本文为系列文章的第2篇,主要讲解对图像的像素的操作方法. 2.1存取像素值 为了存取矩阵元素,需要指定元素所在的行和列,程序会返回相应的元素.单通道图像返回单个数值,多通道图像,返回的则是一组向量(V ...
- hdu3622
hdu3622 题意 每回合给定两个坐标点,可以选择一个放置炸弹,自己决定炸弹的半径,问 n 个回合后,使炸弹半径最小值最大. 分析 存在对立关系:每回合只能选择一个地方放置炸弹.i 表示 第一个位置 ...
- 生成JSON数据--官方方法
官方生成方法: 1)需要什么就给什么,要属性就给属性,要对象就给对象,要集合就给集合 2)添加都是使用put()方法 要求: 1.生成如下JSON数据: {"age":4,&quo ...
- centos 下 安装mysql
今天在centos上安装了一下 mysql 出现了一点问题 记录一下解决方案: 1:解决yum install mysql-server没有可用包的问题 sudo yum install mysql- ...