【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 ...
随机推荐
- WPF绑定功能常用属性介绍
1.Mode 绑定中数据流的方向(enum BindingMode) 目标属性指的是控件的属性 (1)TwoWay 更改源属性或目标属性时,会自动更新另一方.适用于可编辑窗体 例:TextBox (2 ...
- vue 后台获取文件流导出excel文件
let params = { compStartTm: Date.parse(this.searchForm.compStartTm) / 1000, compEndTm: Date.parse(th ...
- vue2 v-for
- Django ManyToManyField.through_fields 和
示例: from django.db import models class Person(models.Model): name = models.CharField(max_length=50) ...
- maven 安装第三方jar到本地 出现 The goal you specified requires a project to execute but there is no POM in this directory 错误
原因是因为操作系统的差异导致,把所有参数加上引号即可. 如下所示: mvn install:install-file "-Dfile=cobra.jar" "-Dgrou ...
- Java8-LongAccumulator
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- 网络和Web编程
一.以客户端的形式同HTTP服务交互 (1)使用urllib.request模块发送HTTP GET请求 from urllib import request,parse url = 'http:// ...
- win10以及ubuntu下设置pip源
问题描述:有一段时间下载python库的时候速度非常慢,想着提高安装python库的速度. window10下: 一:首先进入c盘的用户目录,如我的目录为C:\Users\felix. 二:创建名为p ...
- devstack 使用openstack命令报错 The request you have made requires authentication. (HTTP 401) Missing value auth-url required for auth plugin password
关联错误: The request you have made requires authentication. (HTTP 401) (Request-ID: req-88ad2cba-0f2d-4 ...
- iOS Jenkins 自动化打包构建
前言 在测试app项目过程中,通常都是需要开发打测试包给到测试,但是无论是iOS还是Android的打包过程都是相当漫长的,频繁的回归测试需要频繁的打包,对于开发同学影响还是蛮大的.因此在这种情况下, ...