反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。

示例 1:

输入: "Let‘s take LeetCode contest"
输出: "s‘teL ekat edoCteeL tsetnoc" 
注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。

code1:使用自定义reverse函数

class Solution {
public:
    string reverseWords(string s) {
        if(s.empty())
            return "";

        for(int i=0;i<s.size();)
        {
            if(s[i]==‘ ‘)
                continue;
            int j=i+1;
            while(s[j]!=‘\0‘&&s[j]!=‘ ‘)
                ++j;
            --j;
            reverseWordsCore(s,i,j);
            i=j+2;
        }
        return s;
    }
private:
    void reverseWordsCore(string &s,int i,int j)
    {
        while(i<=j)
            swap(s[i++],s[j--]);
        return;
    }
};

code2:使用stl中reverse

class Solution {
public:
    string reverseWords(string s) {
        if(s.empty())
            return "";

        for(int i=0;i<s.size();)
        {
            if(s[i]==‘ ‘)
                continue;
            int j=i+1;
            while(s[j]!=‘\0‘&&s[j]!=‘ ‘)
                ++j;
            reverse(s.begin()+i,s.begin()+j);
            i=j+1;
        }
        return s;
    }
};

原文地址:https://www.cnblogs.com/tianzeng/p/11994053.html

时间: 2024-08-27 08:59:49

反转字符串中的单词 III的相关文章

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

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

Leetcode 557.反转字符串中的单词III

反转字符串中的单词III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 实现思路: 刚看题目心想直接用String.split会不会更容易些,不过这样就失去了这个算法的意义了.还是采用最原始的方法,先

557. 反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路:先将给定的字符串(s)中的单词拆分出来(str),然后单个处理每个单词,拼接成一个新的字符串(ans),返回ans. 总结: 1 static co

[LeetCode 557] 反转字符串中的单词 III

题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/reverse-words-in-a-stri

【LeetCode-面试算法经典-Java实现】【152-Reverse Words in a String(反转字符串中的单词)】

[152-Reverse Words in a String(反转字符串中的单词)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

反转字符串中的单词

第一种 export default (str) => { // 字符串按空格进行分隔,保存数组,数组的元素的先后顺序就是单词的顺序 let arr = str.split(' ') // 对数组进行遍历,然后每个元素进行反转 let result = arr.map(item => { return item.split('').reverse().join('') }) return result.join(' ') } 第二种 export default (str) => { /

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间