【LeetCode刷题Java版】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".

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.

package com.liuhao.acm.leetcode;

/**
 * @author liuhao
 *
 *         Given an input string, reverse the string word by word. For example,
 *         Given s = "the sky is blue", return "blue is sky the".
 */
public class ReverseWords {

	public static String reverseWords(String s) {

		// 若输入字符串直接是空串,则直接返回该字符串
		if (s.equals("")) {
			return "";
		}

		// 将字符串按空格"\\s{1,}"进行分割,一个或者多个空格的正则:"\\s{1,}"
		String[] strArr = s.split("\\s{1,}");
		int len = strArr.length;

		// 分割后字符串变成空串,直接返回
		if (len == 0) {
			return "";
		}

		// 用StringBuilder更加有效
		StringBuilder sb = new StringBuilder("");

		// 反向输出之前分割的字符串数组
		for (int i = len - 1; i >= 0; i--) {
			if (!strArr[i].equals("")) {
				sb.append(strArr[i]);
				sb.append(" ");
			}
		}

		sb.deleteCharAt(sb.lastIndexOf(" "));

		return sb.toString();
	}

	public static void main(String[] args) {
		System.out.println(reverseWords(""));
	}

}
时间: 2024-08-29 14:22:31

【LeetCode刷题Java版】Reverse Words in a String的相关文章

【LeetCode刷题Java版】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", "1", "+", "3", "*"] -&g

【LeetCode刷题Java版】Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. package com.liuhao.acm.leetcode; /** * @a

【leetcode刷题笔记】Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

【leetcode刷题笔记】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【leetcode刷题笔记】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", "1", "+", "3", "*"] -&g

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode刷题笔记】Plus One

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题解:模拟即可,用carries代表进位,当carries=1的时候说明下一位上要加上进位,就一直往前进位,直到某一位可以加1不进位,或者已经到最高位.

【leetcode刷题笔记】ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line:  "PAHNAPLSI