【Leetcode】Reverse Words in a String JAVA实现

一、题目描述

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

二、分析

要注意几点:1、当字符串的头部或者尾部存在空格时,最后都将被消除

      2、当两个子字符串之间的空格的个数大于1时,只要保留一个

解题思路:1、首先,将整个字符串进行反转

     2、然后,使用split函数对字符串进行切割;并将得到每个字符串进行反转

     3、最后,在得到的字符串的头部和尾部的空格使用trim()函数去除

三、代码实现

public class Solution {

	public String reverse(String s){                //这是对一个字符串的反转,从尾到头依次添加到StringBuffer中去
		if(s.length()<=1){
			return s;
		}
		StringBuffer sb =new StringBuffer();
		for(int i=s.length()-1;i>=0;i--){
			sb.append(s.charAt(i));
		}
		return sb.toString();
	}
	/*
	 * 根据字符串中的空格进行分割,并对每个子字符串进行反转并在后面添加空格
	 */
	public String reverseWords(String s) {
		s=reverse(s);
		String[] str=s.split(" ");                        //分割
		StringBuffer sb =new StringBuffer();
			for(int i=0;i<str.length;i++){
				if(!str[i].equals("")){
					sb.append(reverse(str[i])).append(" ");         //将字符串反转并添加到StringBuffer中去
				}
			}
		return sb.toString().trim();
    }

	public static void main(String[] args) {
		String s = " ";
		Solution st= new Solution();

		String str=st.reverseWords(s);
		System.out.println(str);

	}

}

  四、如果要保留字符串中所有的空格的做法

public class Solution {

	public String reverse(String s){                //这是对一个字符串的反转,从尾到头依次添加到StringBuffer中去
		if(s.length()<=1){
			return s;
		}
		StringBuffer sb =new StringBuffer();
		for(int i=s.length()-1;i>=0;i--){
			sb.append(s.charAt(i));
		}
		return sb.toString();
	}
	/*
	 * 根据字符串中的空格进行分割,并对每个子字符串进行反转并在后面添加空格
	 */

	public String reverseWords(String s) {
		s=reverse(s);
		String[] str=s.split(" ");
		//System.out.println(str.length);
		StringBuffer sb =new StringBuffer();
		if(str.length!=0){
			for(int i=0;i<str.length-1;i++){     //
				if(!str[i].equals("")){                   //用于判断当前是是不是空,如果是,则添加空格,如果不是,则反转字符串
					//str[i]=reverse(str[i]);
					sb.append(reverse(str[i])).append(" ");
				}
				else{
					sb.append(" ");
				}
			}
			sb.append(reverse(str[str.length-1]));   //将最后一个字符串进行反转,并添加到StringBuffer中
		}

		//System.out.println(s.length());
		//System.out.println(sb.length());
		if(sb.length()!=s.length()){
			/*
			 * 因为字符串进行切割时,会将后来的空格遗弃,(String ="zhao  yan     ",该字符串进行切割时,yan后面的空格将被忽略)
			 * 所以当得到的字符串长度不等于原字符串长度时,在后面补空格
			 */
			int len=s.length()-sb.length();
			for(int j=0;j<len;j++){
				sb.append(" ");
			}
		}
		//System.out.println(s.length());
		//System.out.println(sb.length());
		return sb.toString();
    }

	public static void main(String[] args) {
		String s = " ";
		Solution st= new Solution();

		String str=st.reverseWords(s);
		System.out.println(str);

	}

}

  

时间: 2024-12-27 22:15:19

【Leetcode】Reverse Words in a String JAVA实现的相关文章

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

Reverse Words in a String (JAVA)

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 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 151. Reverse Words in a String --------- java

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. 倒序字符串. 注意使用BufferString,因为如果使用Stri

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