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 ...
随机推荐
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第三篇(登录窗口的实现以及如何保存登录者的信息)
一.前言: 1.再看这篇文章的时候,您是否已经完成前两篇介绍的文章里的功能了?(Tabs页的添加,Tabs页右键的关闭,主题的更换) 2.今天来说说登录窗口吧,看截图: ...
- 《Linux内核设计与实现》读书笔记(二)- 内核开发的准备
在尝试内核开发之前,需要对内核有个整体的了解. 主要内容: 获取内核源码 内核源码的结构 编译内核的方法 内核开发的特点 1. 获取内核源码 内核是开源的,所有获取源码特别方便,参照以下的网址,可以通 ...
- Android性能优化系列---管理你的app内存
文章出处:http://developer.android.com/training/articles/memory.html#YourApp Random-access memory(RAM)在任 ...
- [Django笔记] Apache + mod-wsgi 环境部署所遇到的各种问题总结
在一台CentOS7机器上配置Django+apache运行环境 Django安装 python2 or python3 ? 一般情况下Linux系统都有自带python2,本机CentOS7上的是p ...
- 洛谷P1072 Hankson 的趣味题
P1072 Hankson 的趣味题 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一 ...
- Mysql-6-数据类型和运算符
1.mysql数据类型 (1)数值数据类型:包括整数类型tinyint.smallint.mediumint.int.bigint,浮点小数类型float和double,定点小数类型decimal. ...
- myclipse运行web的一些问题
一.修改项目访问路径 项目右键>properties(属性)>输入web搜索>双击web>修改Web-Content root内容即可 二. myeclipse中web项目不自 ...
- java socket 网络通信 指定端口的监听 多线程 乱码
Java Socket编程 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了.首 ...
- 当重装eclipse后,给现有web项目添加tomcat的构建路径
在eclipse“首选项”-“service environment”中配置好tomcat后,给现有web项目添加构建路径: 1.选中一个web项目右键选中“构建路径”-“配置构建路径”
- sourceInsight4 破解笔记(完美破解)
https://www.cnblogs.com/Napoleon-Wang/p/6706773.html 时隔好多年,sourceinsight4以迅雷不及掩耳之势的来了.与3.5相比,sourcei ...