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-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.
public class Solution {
    public String reverseWords(String s) {
        if(s.length() == 0) {
            return s;
        }
        Stack<String> stack = new Stack<String>();
        String[] ss = s.split("\\s+");
        for(String word : ss) {
            stack.push(word);
        }
        StringBuilder sb = new StringBuilder();
        while(!stack.isEmpty()) {
            sb.append(stack.pop()).append(" ");
        }
        return sb.toString().trim();
    }
}

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

时间: 2024-08-04 14:17:35

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

[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] 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". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

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

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

/*测试数据: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){/

软件测试第二次作业 - 写一个Java程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。

题目一: 1. 写一个Java程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示.(单词之间用空格隔开,如“Hello World My First Unit Test”): 2. 编写单元测试进行测试: 3. 用ElcEmma查看代码覆盖率,要求覆盖率达到100%. Demo类: 1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4 import java.

leetcode python翻转字符串里的单词

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

从键盘输入一个字符串,按照字符顺序从小到大进行选择排序,并要求删除重复的字符

/* 从键盘输入一个字符串,按照字符顺序从小到大进行选择排序,并要求删除重复的字符 思路: 选择排序:比较找到最小的下标,和第i个交换位置. 删除重复字符:用k计算不相等的个数,替换. */ #include <stdio.h> #include<string.h> void insetsort(char *str,int n){ int i,j,min,temp,k; char sh[n]; for(i=0;i<n;i++){ min=i; for(j=i+1;j<n

判断一个字符串里是否含有某段字符?怎么截取一段字符?

写前端过程中遇到的最多的字符串操作莫过于 :判断一个字符串里是否含有某段字符 ,和 截取一段字符串. 字符串操作有很多方法,其实一般只要掌握以上两个就够用了,其他方法随他去吧,好,下面就以上两个方法讲解一下. 1.判断一个字符串是否含有某段字符,使用indexOf()方法: str.indexOf("参数1","参数2");参数1表示判断是否包含的小字符串, 参数2表示从左到有依次判断的起始位置,默认从0开始,str表示用于寻找的原字符串,ps:如果原字符串含多个判

判断一个字符串里是否包含了另一个字符串

NSString *str1 = @"comeontom哈哈回复好 sad 分身乏术的发烧发烧的"; NSString *str2 = @"comeontom"; if ([str1 rangeOfString:str2].location != NSNotFound) { NSLog(@"str1包含str2"); }else{ NSLog(@"str1不包含str2"); } 判断一个字符串里是否包含了另一个字符串