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 ...
随机推荐
- 关于github改名问题
不喜欢github显示的目录名字于是百度了下,更改过程,记录下来,方便日后查看! 首页右上角点击出来菜单,找到Settings按钮点击 左侧找到Account账号菜单点击 找到change usern ...
- 基础架构之Mongo
项目需求中,有些需求的数据是不必长时间持久化或一些非结构化设计,这时可以考虑用Mongo作为存储,具体介绍介绍详见官方 https://www.mongodb.com,这篇文章主要介绍安装及启用身份认 ...
- How to Build MySQL from Source Code on Windows & compile MySQL on win7+vs2010
Not counting obtaining the source code, and once you have the prerequisites satisfied, [Windows] use ...
- mysqlcppconn之ConnectOptionsMap的使用
由来 继上一篇文章, 发现之前写的一篇文章中断线重连部分是错误的, 也是现在翻阅了源码才知道 想要自动重连, 必须使用ConnectOptionsMap才可以 但由于官方代码没有做好导出部分的处理, ...
- Oracle基础之分析表
analyze table tablename compute statistics; analyze index indexname compute statistics; (analyze 不会重 ...
- 如何为Android平台编译 opencv3 和 opencv_contrib (Linux)
编译出来的opencv库有问题,正在调试中 ...... 本文以编译opencv 3.3.0 和 opencv_contrib 3.3.0为例,系统为 Linux x64 (Fedora 21),具体 ...
- 使用C++11新特性来实现RAII进行资源管理
方法一:借助auto.decltype.unique_ptr.Lambda表达式构造 sqlite3 *db = NULL; auto deleter = [](sqlite3 *pdb){sqlit ...
- mac 修改MAC代码
1.生成一个mac地址: openssl rand -hex | sed 's/\(..\)/\1:/g; s/.$//' 2.关闭无线 sudo /System/Library/PrivateFra ...
- git revert .vs. git reset .vs. git rebase
1. git rervert的工作方式是:将一个老的commit的改动完全找出来,并且在新的tip处运行反操作,最终清除老commit的改动: git revert的应用场景多在对public rep ...
- python多进程与协程
1.进程的概念 什么是进程->CPU在同一时刻只能处理一个任务,只是因为cpu执行速度很快. cpu在各个任务之间来回的进行切换. 进程的概念:正在进行的一个过程或者说一个任务,而负责执行任务的 ...