LeetCode Reverse Words in a String 将串中的字翻转
class Solution {
public:
void reverseWords(string &s) {
string end="",tem="";
char *p=&s[];
while(*p!='\0'){
while(*p==' ') //过滤多余的空格,针对串头
p++;
while(*p!=' '&&*p!='\0'){ //积累一个单词,存于临时串
tem=tem+*p;
p++;
}
while(*p==' ') //过滤多余的空格,针对串尾
p++;
if(*p!='\0') //最后一个字不用加空格
tem=' '+tem;
end=tem+end;
tem=""; //临时字符串清空
}
s=end;
}
};
题意:将字符串中的字按反序排列,每个字中间有一个空格,串前和串尾无空格。字的顺序不用改变,改变的是字在串中的顺序。
思路:过滤串的前面和后面的空格,用指针从前往后扫, 再用一个临时串保存字,满一个字的时候就添加在将最终的串的前面。扫完该串就将最终的串赋给s。
LeetCode Reverse Words in a String 将串中的字翻转的更多相关文章
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [leetcode] Reverse Words in a String [1]
一.题目: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
随机推荐
- Tomcat之NIO 启动与应用分析
概述 从入门Web开始一直在使用Tomcat,随着对网络相关的知识的进一步了解,觉得越有必有去阅读一下常用的开源服务器的整个工作流程,以及使用场景,对比几款服务器的优劣势.最终根据合适的业务场景进行优 ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- C# BackgroundWorker(异步线程)
日期:2018年11月28日 环境:Windows 10,VS2015 前言 .NET 类库中提供了一个快捷使用多线程的帮助类BackgroundWorker,能够快速创建一个新的线程,并能报告进度 ...
- 【C#】清除webBrowser 缓存和Cookie的解决方案
试了很多方法,最后发现万剑大哥的方法管用,转载一下 转自:https://www.cnblogs.com/midcn/p/3527123.html 通过测试webBrowser与IE缓存和Cookie ...
- Python实现返回指定范围内的所有素数
# 获取a, b范围的所有素数 def func(a, b): li = [] for i in range(a, b+1): for j in range(2, i): if i % j == 0: ...
- Perfect service(树形dp)
Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...
- AX 2012 窗体增加分隔线
在AX中将窗体控件分区一般通过group来实现,但是类似salesTable的可以调整大小的分区其实也是用group控件再加一些方法实现的. 1, 留意splitGroup属性: Autodeclar ...
- 洛谷 P3128 [USACO15DEC]最大流Max Flow
题目描述 \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤10 ...
- 洛谷P1318 积水面积
题目描述 一组正整数,分别表示由正方体叠起的柱子的高度.若某高度值为\(x\),表示由\(x\)个正立方的方块迭起(如下图,\(0<=x<=5000\)).找出所有可能积水的地方(图中蓝色 ...
- Maven下把父项目下的子项目导出到myeclipse中
第一种在父项目下已有子项目:右击空白------import 第二步Maven4MyEclipse-----------Existing Maven Projects 第三部选择父项目下面的子项目 ...