LeetCode OJ-- Reverse Words in a String
https://oj.leetcode.com/problems/reverse-words-in-a-string/
给一个字符串 abc dd m,返回 m dd abc.
注意:输入中可能有前置或者后置空格,要求,都删除掉。输出的每个单词之间,空格大小为1.
class Solution {
public:
void reverseWords(string &s) {
if(s.empty())
return;
//remove heading and trailing spaces
int i = ;
while(i<s.size() && s[i] == ' ')
i++;
if(i == s.size())
{
s = "";
return;
}
int j = s.size() - ;
while(j>- && s[j] == ' ')
j--;
if(j == -)
{
s = "";
return;
}
s = s.substr(i,j - i + );
size_t pos = ;
vector<string> strs;
size_t begin = ;
while(begin < s.size())
{
pos = s.find_first_of(' ',begin);
if(pos == begin)
{
begin++;
continue;
}
else if(pos != -)
strs.push_back(s.substr(begin,pos - begin));
else //pos == -1, the end
{
strs.push_back(s.substr(begin,s.size() - - begin + ));
break;
}
begin = pos + ;
}
string ans;
for(int i = strs.size() - ; i > ; i--)
{
ans += strs[i];
ans += " ";
}
ans += strs[];
s = ans;
}
};
LeetCode OJ-- 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& ...
- [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(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 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 - [1]Reverse Words in a String
Question: Reverse Words in a String Given an input string, reverse the string word by word. For exam ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- LeetCode 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...
- Python [Leetcode 345]Reverse Vowels of a String
题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
随机推荐
- CTU Open Contest 2017
这场题很水.水题我就懒得贴了. B - Pond Cascade 优先队列维护这个水池需要多少时间 或者 直接扫一遍. #include <cstdio> #include <cst ...
- 实验一 查看CPU和内存,用机器指令和汇编指令编程
(1):使用debug,将下面的程序段写入内存,逐条执行,观察每条指令执行后,CPU中相关寄存器中内存的变化. 机器码 汇编指令 b8 20 4e mov ax,4E20H 05 ...
- P1880 [NOI1995]石子合并【区间DP】
题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...
- 数据库sql中distinct用法注意事项
在写sql中去重复等操作,需要用到distinct. 在使用distinct的时候要注意,尤其是在有行列转换的时候.要把sql运行出来看看是不是与你想要的结果一样. 通过自己试验,distinct有从 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 用js立即执行函数开发基于bootstrap-multiselect的联动参数菜单
代码调用方式如下: data=[{F0:总分类cd,F1:总分类name,F2:大分类cd,F3:大分类name,F4:中分类cd,F5:中分类name,F6:小分类cd,F7:小分类name},.. ...
- Python框架之Django学习笔记(十一)
话说上次说到数据库的基本访问,而数据库我们主要进行的操作就是CRUD,也即是做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete),俗 ...
- leetcode 【 Partition List 】python 实现
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Bit Operation妙解算法题
5道巧妙位操作的算法题. ***第一道*** 题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. ...