LeetCode 翻转字符串里的单词

给定一个字符串,逐个翻转字符串中的每个单词。

示例 1:

输入: "the sky is blue"
输出: "blue is sky the"

示例 2:

输入: "  hello world!  "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。

示例 3:

输入: "a good   example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

解法:按要求模拟,首先前面有" "的都要去了,然后开始就是单词了,每碰到一个" "就是一个单词,用栈存储起来就可以了。然后输出的时候从栈当中拿出来输出即可。
class Solution {
public:
    string reverseWords(string s) {
        stack<string> str;
        string s0 = "";
        if(s.empty())
        {
            s = "";
            return s;
        }
        for(int i=0;i<s.length();i++)
        {
            if(s[i]!=‘ ‘)
            {
                s0+=s[i];
                continue;
            }  //得到字符组成字符串。
            else if(!s0.empty())
            {
                str.push(s0);
                s0="";
            }
        }
        if(!s0.empty())
        {
            str.push(s0);
            s0="";
        }
        while(!str.empty())
        {
            s0+=str.top();
            str.pop();
            s0+=" ";
        }
        if(s0.empty())
        {
            s = "";
            return s;
        }
        s0.erase(s0.end()-1);
        s = s0;
        return s;
    }
};


原文地址:https://www.cnblogs.com/jkzr/p/10594916.html

时间: 2024-07-29 23:26:14

LeetCode 翻转字符串里的单词的相关文章

leetcode python翻转字符串里的单词

# Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: "blue is sky the" **示例2:** 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. **示例3:** 输入: "

LeetCode 151 翻转字符串里的单词

题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: "  hello world!  " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 示例 3: 输入: "a good   example" 输出: "e

151 Reverse Words in a String 翻转字符串里的单词

给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用O(1) 时间复杂度的原地解法.说明:    什么构成一个词?    一系列非空格字符组成一个词.    输入字符串是否可以包含前导或尾随空格?    是.但是,您的反转字符串不应包含前导或尾随空格.    两个单词之间多空格怎么样?    将它们缩小到反转字符串中的单个空格.详见:https://leetc

leetcode151. 翻转字符串里的单词

给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue"输出: "blue is sky the" 示例 2: 输入: "  hello world!  "输出: "world! hello"解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 示例 3: 输入: "a good   example"输出: "example g

151.翻转字符串里的单词

class Solution: def reverseWords(self, s: str) -> str: if s == '': return s ls = s.split() # " " if ls == []: return '' result = '' for i in range(0, len(ls)): result += ls[-1 - i] + ' ' # strip()去除首尾空格 result = result.strip() return result 原

翻转字符串里的单词

试题地址:https://leetcode-cn.com/problems/reverse-words-in-a-string/ 试题思路: go自带strings.Fields()函数,可以剔除多余空格 试题代码: func reverseWords(s string) string { strList := strings.Fields(s) if len(strList) == 0 { return "" } reverseS := "" for i := l

Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode

LeetCode新题,但是比较简单,直接用栈即可 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". click to show clarification. Clarification: What constitutes a word?A sequence of non-sp

[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

力扣(LeetCode)字符串中的单词数 个人题解

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 题目描述比较不清楚,这里只要是用空格隔开的一律当作字符,包括非字母.使用JAVA自带库函数解决问题.记得忽略空格情况 当然这里使用了较大的内存保存分割后的ss字符串数组,如果对内存比较敏感的可以对字符串手动以空格划分.(这里空格可能多个,所以可以使用正则表达式较为方便去匹配) 代码如下: cl