LeetCode——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".

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.

中文:给定一个输入字符串,依照单词逆转之。

比如:

给定 s="the sky is blue",

返回 "blue is sky the"

说明:什么组成一个单词?

一个非空的字符序列组成一个单词。

输入的字符串能够包括头或尾的空格吗?

能够。可是你的倒转的字符串不应该包括头尾空格。

两个单词间有多个空格又如何呢?

在倒转的字符串中把它们降低为一个空格。

此题比較简单,主要考虑使用空格进行切割和空格的去除。

Java:

	  public String reverseWords(String s) {
		  if(s.trim() == "")
			  return null;
	        String str[] = s.trim().split("\\s+");
	        StringBuilder sb = new StringBuilder();
	        int len = str.length;
	        for(int i=len-1;i>=0;i--){
	        	if(str[i] == " ")
	        		continue;
	        	sb.append(str[i]+" ");
	        }
	        return sb.toString().trim();
	    }

LeetCode——Reverse Words in a String

时间: 2024-10-10 14:38:16

LeetCode——Reverse Words in a String的相关文章

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q

[leetcode]Reverse Words in a String @ Python

原题地址:https://oj.leetcode.com/problems/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". 解题思路:这题很能体现python处理字符串的强大功能. 代码: class Solut

[LeetCode] Reverse Words in a String 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

[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——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: 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 c

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: 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: Inpu

[LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

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"

LeetCode: Reverse Words in a String && Rotate Array

Title: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". https://leetcode.com/problems/reverse-words-in-a-string/ 思路:先将字符串全部逆转,在将每个单词逆转.具体实现,要注意空格,所以对字符串倒过来处理. cla

Leetcode: 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". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input