【leetcode_easy】557. Reverse Words in a String III
problem
557. Reverse Words in a String III
solution1:字符流处理类istringstream.
class Solution {
public:
string reverseWords(string s) {
string ans = "", t = "";
istringstream is(s);//
while(is >> t)
{
reverse(t.begin(), t.end());
ans += t + " ";//err.
}
ans.pop_back();
return ans;
}
};
solution2:单词首尾指针。
class Solution {
public:
string reverseWords(string s) {
int start = , end = ;
while(start<s.size() && end<s.size())
{
while(end < s.size() && s[end]!=' ') end++;
for(int i=start, j=end-; i<j; i++, j--)//
{
swap(s[i], s[j]);//
}
start = ++end;
//start = end + 1;
//end++;
}
return s;
}
};
参考
1. Leetcode_easy_557. Reverse Words in a String III;
2. Grandyang;
完
【leetcode_easy】557. Reverse Words in a String III的更多相关文章
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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#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 ...
- 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 ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 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 ...
随机推荐
- 2018/7/31-zznu-oj-问题 F: 手机密码--【裸dfs+for循环即可!——据说三个小时内只有两个人读完了题意并轻松AC了】
问题 F: 手机密码 时间限制: 1 Sec 内存限制: 128 MB提交: 41 解决: 23[提交] [状态] [讨论版] [命题人:admin] 题目描述 手机安全问题是大家经常关心的一个问 ...
- Cairo初探
https://blog.csdn.net/flexwang_/article/details/38000401 二维解析pdf
- go语言defer关键字背后的实现,语法,用法
原文: https://tiancaiamao.gitbooks.io/go-internals/content/zh/03.4.html ------------------------------ ...
- Mybatis 动态SQL注解 in操作符的用法
在SQL语法中如果我们想使用in的话直接可以像如下一样使用: ,,) ; ,,) ; 但是如果在MyBatis中的使用 in 操作符,像下面这样写的话,肯定会报错: @Update("upd ...
- Android笔记-Activity相关+内存泄漏+Fragment+service
看了下,上次学习android还是17年的事情,,,,两年过去了我现在终于来搞android了... 官网有一段基础描述: https://developer.android.google.cn/gu ...
- Oracle12c-ADG搭建
实验环境: 角色 IP hostname CDB name db_unique_name pdb name 版本 主 192.168.0.115 Node11 cdb1 cdb_p pdb1 12.2 ...
- C指针与数组
通过指针遍历数组的小把戏 #include<stdio.h> #include<stdlib.h> #include<Windows.h> void main() ...
- Django传递数据给JS
这里讲述两种方法: 一,页面加载完成后,在页面上操作,在页面上通过 ajax 方法得到新的数据(再向服务器发送一次请求)并显示在网页上,这种情况适用于页面不刷新的情况下,动态加载一些内容.比如用户输入 ...
- python自动华 (四)
Python自动化 [第四篇]:Python基础-装饰器 生成器 迭代器 Json & pickle 目录: 装饰器 生成器 迭代器 Json & pickle 数据序列化 软件目录结 ...
- Linux下shell命令执行过程简介
Linux是如何寻找命令路径的:http://c.biancheng.net/view/5969.html Linux上命令运行的基本过程:https://blog.csdn.net/hjx5200/ ...