557. Reverse Words in a String III (5月25日)
解答
class Solution {
public:
string reverseWords(string s) {
string temp,result;
while(1){
if(s.find(' ')!=string::npos){
temp=s.substr(0,s.find(' '));
reverse(temp.begin(),temp.end());
result += temp+" ";
s=s.substr(s.find(' ')+1);
}
else{
temp=s;
reverse(temp.begin(),temp.end());
result += temp;
break;
}
}
return result;
}
};
笔记
- 循环的问题,我之前是用s.empty()判断来作为判断条件的,结果会进入死循环。
- 找不到空格说明已经只剩最后一个单词了,需要单独处理,
- 找到空格时创建新串要加上空格
result += temp + " ";
补充
- 其实可以用s.empty()来作为循环的条件,处理方式为:
条件: 1 改为 !s.empty()
else中: break 改为 s.clear()
557. Reverse Words in a String III (5月25日)的更多相关文章
- 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
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 ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- 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 ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
- [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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
随机推荐
- oracle数据库基本命令
数据库字符集: SQL> select * from nls_database_parameters where parameter='NLS_CHARACTERSET'; PARAMETER ...
- 基础架构之Maven私有库
Maven对于Java开发来说肯定不会陌生,由于各种问题,公司常常需要搭建自己的私有Maven仓库. (一) 环境要求 Centos 7.5.1804 Docker 18.06.1-ce sonat ...
- 使用SlidingPaneLayout 实现仿微信的滑动返回
上周,公司的项目改版要求加上一个右滑返回上一个界面,于是就在网上找了一些开源库打算实现.但是在使用的时候遇见了许多的问题.试了两天用过 https://github.com/ikew0ng/Swipe ...
- 题目一:使用Java实现二维数组中的查找
考点:数组 题目:二维数组中的查找 描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判 ...
- pymsql模块使用
数据库连接客户端 链接:https://pan.baidu.com/s/1pM0h4SV 密码:614v sql指令基本用法:
- Linux->Ubuntu配置tomcat开机自动启动
Ubuntu配置tomcat开机自动启动 我们有时候会有这样一个需求: 在开机的时候就启动一个服务,比如tomcat. 我们可以这样做: 将tomcat目录下/bin中的catalina.sh拷贝到/ ...
- shell单引号双引号详解
linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了) " "(双引号)与 ' '(单引号)的区别 你在shell prompt(shell 提示)后面敲 ...
- [原]Linux 命令行浏览器
真是没有做不到只有想不到! Linux下竟然有命令行式的浏览器:W3m SPC向下翻页 b向上翻页 J 向下滚动一行 K 向上滚动一行 > 右移一屏 < 左移一屏 TAB 转到下个超链接 ...
- 记一次挖掘115网盘反射型xss,08xss的储存型xss
记一次对115分站简单绕过过滤继续实现xss,08xss平台也中枪!! 115反射型xss url:http://115.qiye.115.com/disk/?ac=select_public_fil ...
- 十.mysqld_multi stop无效问题
今天在尝试运行mysqld_report stop的时候,发现无法停止mysql,日志中的错误如下 Stopping MySQL servers mysqladmin: [Warning] Using ...