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 String III(反转字符串中的单词 III)
 * 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
 */
public class Solution557 {
    public static void main(String[] args) {
        Solution557 solution557 = new Solution557();
        String s = "Let's take LeetCode contest";
        System.out.println(solution557.reverseWords(s));
    }

    /**
     * 分割字符串,再逆序,拼接到字符串
     *
     * @param s
     * @return
     */
    public String reverseWords(String s) {
        String[] words = s.split(" ");
        StringBuilder sb = new StringBuilder();
        int len = words.length;
        if (words.length == 1) {
            return reverseString(s);
        }
        for (int i = 0; i < len - 1; i++) {
            sb.append(reverseString(words[i]) + " ");
        }
        sb.append(reverseString(words[len - 1]));
        return String.valueOf(sb);
    }

    public String reverseString(String s) {
        char[] chars = s.toCharArray();
        int i = 0;
        int j = chars.length - 1;
        while (i < j) {
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
            i++;
            j--;
        }
        return new String(chars);
    }
}

原文地址:https://www.cnblogs.com/wupeixuan/p/9577368.html

时间: 2024-12-28 16:33:36

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

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

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

【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". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

[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

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

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

反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest"输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. code1:使用自定义reverse函数 class Solution { public: string reverseWords(string s) { i

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

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 - 557. Reverse Words in a String III (C++) stringstream

1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是要注意空格的影响.比方说,string首尾和单词之间可能有一或多个空格.看到有人逐个对空格判断,但是我觉得逐个判断其实稍微容易出错(当然如果非常熟悉的话就完全无所谓啦),我的一个简单想法是用stringstream. PS:不熟悉stringstream的朋友可以看看链接的文档. 3. 代码 cla

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 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" O