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的更多相关文章

  1. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  2. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  3. 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 ...

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 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 ...

  6. 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 ...

  7. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  8. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  9. 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 ...

随机推荐

  1. Paper Reading:Receptive Field Block Net for Accurate and Fast Object Detection

    论文:Receptive Field Block Net for Accurate and Fast Object Detection 发表时间:2018 发表作者:(Beihang Universi ...

  2. python3 虚拟环境

    一.python中的虚拟环境 1.虚拟环境:局部的,独立的python环境,完全模拟系统全局python环境的使用 二.安装 http://virtualenv.pypa.io/en/latest/u ...

  3. 微信小程序将图片数据流添加到image标签中

    原文:https://blog.csdn.net/OliveLao/article/details/78136121 ---------------------------------------- ...

  4. C语言学习系列(四)C语言基本语法和数据类型

    一.基本语法 C的令牌(Tokens) C 程序由各种令牌组成,令牌可以是关键字.标识符.常量.字符串值,或者是一个符号. 关键字(保留字) auto else long switch break e ...

  5. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  6. 016_STM32程序移植之_舵机

    STM32程序移植之舵机PWM测试 接线图如下: STM32引脚 舵机引脚 功能 GND GND 正极电源 具体看舵机的额定电压 PA6 PWM引脚 STM32引脚 CH340引脚 GND GND 3 ...

  7. Activiti服务类- IdentityService服务类

    转自:https://www.cnblogs.com/liuqing576598117/p/9815013.html 一.内置用户组(角色)设计表概念 用户和组(或者叫做角色),多对多关联,通过关联表 ...

  8. Java核心数据结构(List、Map、Set)原理与使用技巧

    JDK提供了一组主要的数据结构实现,如List.Set等常用数据结构.这些数据都继承自java.util.Collection接口,并位于java.util包内. 一.List接口 最重要的三种Lis ...

  9. Django-常用异常

    1 from rest_framework.authentication import BasicAuthentication raise AuthenticationFailed(res.dict) ...

  10. 【线性代数】7-1:线性变换思想(The Idea of a Linear Transformation)

    title: [线性代数]7-1:线性变换思想(The Idea of a Linear Transformation) categories: Mathematic Linear Algebra k ...