[lintcode 2 easy Reverse Words in a String]

Problem

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

这题在不考虑multi space空格的情况下的思路:将string全部反转,然后将反转后的string words by words反转。  两次反转,in place操作。

如果考虑multi space,必须改变String,因为String对象本身是不可以改变的,所以要用StringBuilder来处理

思路:设置指针指向单词的最后一位,

另一个指针从字符串最后一位向前遍历,碰到空格,则将该单词向后遍历挨个添加至新string中。

public class Solution {
    /**
     * @param s : A string
     * @return : A string
     */
    public String reverseWords(String s) {
        // write your code
       StringBuilder sb = new StringBuilder();
            int end = s.length();
            for (int i = s.length()-1; i >= -1; i--)
            {
                if (i == -1 || s.charAt(i) == ‘ ‘ )
                {
                    if (i + 1 < end)
                    {
                        if (sb.length() > 0)
                            sb.append(" ");
                        for (int j = i + 1; j < end; j++)
                        {
                            sb.append(s.charAt(j));
                        }
                    }
                    end = i;
                }
            }
            return sb.toString();

      }
}
时间: 2024-10-10 05:38:02

[lintcode 2 easy Reverse Words in a String]的相关文章

(Easy) Reverse Vowels of a String - LeetCode

Description: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" Note:The vowels does not

345. Reverse Vowels of a String【easy】

345. Reverse Vowels of a String[easy] 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&q

557. Reverse Words in a String III【easy】

557. Reverse Words in a String III[easy] 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:

LeetCode_345. Reverse Vowels of a String

345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" N

Reverse Words in a String

Problem: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 这个题目叙述的不够精确,有些边界的情况需要考虑.题目的下方给出了Clarification澄清了几个问题,也是面试的时候需要确认的. 第一个问题是什么构成了一个词(word),回答是一串不包含空格(non

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 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]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. click to show clarification.

1. Reverse Words in a String

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-s