Algorithm

【leetcode】557. Reverse Words in a String III

https://leetcode.com/problems/reverse-words-in-a-string-iii/

1)problem

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

2)answer

判断是否为空格,然后每个单词转换。有一个很好用的交换函数swap。

3)solution

class Solution {
public:
    string reverseWords(string s) {
        int begin = 0;
        int end = 0;
        for (int i = 0; i <= s.length(); ++i) {
            //如果遇到了空格,就准备颠倒每个字符串的值
            if (s[i] == ' ' || s[i] == '\0') {
                end = i;
                // 转换每个单词的值,end是单词的结尾长度,j是单词开始长度。
                for (int j = begin; j < end; ++j) {
                    std::swap(s[j], s[--end]);
                }
                begin = i + 1;
            }
        }
        return s;
    }
};

【leetcode】557. Reverse Words in a String III的更多相关文章

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

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

  2. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

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

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

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

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

  5. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  6. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

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

  7. 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...

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

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

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

随机推荐

  1. vue init 解决办法

    /由于windows系统的某方面问题,vue脚手架安装可能会出现第一证书丢失 // 报错:vue-cli · Failed to download repo vuejs-templates/webpa ...

  2. 【JQ】jq动态绑定事件.on()、解绑事件off()

    #JQ 绑定与解绑事件的方法的历史演变 1. jquery1.4 及之前的版本,由.click() 或 .bind()方法绑定的事件,不能适用脚本创建的新元素:即是说页面加载完成后,再动态创建的DOM ...

  3. 设计模式---数据结构模式之职责链模式(Chain of Responsibility)

    一:概念 职责链模式(CoR,Chain of Responsibility)是行为模式之一,该模式构造一系列分别担当不同的职责的类的对象来共同完成一个任务,这些类的对象之间像链条一样紧密相连,所以被 ...

  4. Part-One

    首先,必须要声明一下,这个目录下的所有东西,是我对一本书复习,只是敲出部分代码让自己不至于眼高手低,其中有很多东西可能都是我的个人理解,如果有兴趣的朋友可以看一下,同时也欢迎大家指正. 1.Hello ...

  5. mysql创建用户与pymsql模块

    mysql 创建用户及增删改查 创建mysql 用户 with grant option 表示用户不存在自主创建 grant [ select ……,insert ……| all ] on 库名.表名 ...

  6. sweetalert插件的使用

    sweetalert是一个漂亮的弹窗插件,使用它可以完成各种炫酷的弹窗效果 链接:sweetalert 实例 删除演示 urls.py from django.contrib import admin ...

  7. setDefaultKeyMode设置Activity的五种按键模式

    setDefaultKeyMode (int mode) 用来设置一个Activity的默认的按键模式, mode一共有五种 DEFAULT_KEYS_DISABLE DEFAULT_KEYS_DIA ...

  8. Webpack2学习记录-2

    这篇在 webpack-demo 目前下新建一个 w2 目录,学习 webpack.config.js 及 与 npm scripts 的使用. 1.w2 下新建一个 webpack.config.j ...

  9. Groovy 设计模式 -- 借贷

    借贷模式 http://groovy-lang.org/design-patterns.html#_loan_my_resource_pattern The Loan my Resource patt ...

  10. PPT制作

    0.[整体风格]整体风格统一 界面排版 0.1 字体大小: 0.2 字体颜色: 0.3 字体的种类统一(不是指只取一种字体)) 1.[表达]结构化表达: 2.[取色]取色风格统一: 技巧:主色不超过三 ...