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. CodeForces632E 神奇的多重背包

    https://cn.vjudge.net/problem/333897/origin 万万没想到这题表面上是个多重背包,实际上确实是个多重背包 题意 n种物品每种物品有无限个,每个物品有一个价格,现 ...

  2. InnoDB和MyISAM的区别

    一.索引的实现 我们都知道InnoDB和MyISAM都是B+数的结构,但是它们的实现有点不一样,直接上图: 因此,MyISAM的查询性能会比InnoDB强 如果用InnoDB是必须有主键的,主键建议用 ...

  3. java基本类型及其相互转换

    1.基本数据类型 java中是采用Unicode编码的,其一个字符占用两个字节,故java中字符char可以存储一个中文汉字 整数默认是int类型,浮点数默认是double类型 在定义long类型的变 ...

  4. JAVA-获取 JDK 动态代理生成的 Class 文件

    可指定路径 import sun.misc.ProxyGenerator; import java.io.FileOutputStream; import java.io.IOException; i ...

  5. Windows LTSC、LTSB、Server 安装 Windows Store 应用商店

    下载安装包 打开网址 https://store.rg-adguard.net/ 以 PackageFamilyName 方式搜索 Microsoft.WindowsStore_8wekyb3d8bb ...

  6. 完美解决distinct中使用多个字段的方法

    众所周知,distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字 ...

  7. Linux 内核里的数据结构:双向链表

    原文:https://blog.csdn.net/qq_33487044/article/details/78827260 双向链表 Linux 内核自己实现了双向链表,可以在 include/lin ...

  8. HTML第四耍 超链接标签

    1.HTML 超链接(链接) 一.HTML 超链接 HTML中使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 超链接可以是一个字,一个词 ...

  9. Openresty 学习笔记(二)Nginx Lua 正则表达式相关API

    ngx.re.match 语法: captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) 环境: init_w ...

  10. Spring boot 连接Redis实现HMSET操作

    这篇文章记录使用spring-boot-starter-redis访问Redis.Redis相关的的配置文件放在Resources目录下的application.yml文件中,如下所示: spring ...