lintcode53 Reverse Words in a String -easy

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

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

    • 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
import java.util.Stack;
public class Solution {
    /*
     * @param s: A string
     * @return: A string
     */
    public String reverseWords(String s) {
        // write your code here

        if(s == null){
            return null;
        }

        if(s.length() == 0){
            return new String();
        }

        Stack<String> stack = new Stack<String>();
        int head = 0;
        int tail = 0;

        while(head < s.length()){
            while(head < s.length() && s.charAt(head) == ‘ ‘){
                head++;
            }
            tail = head;

            while(tail < s.length() && s.charAt(tail) != ‘ ‘){
                tail ++;
            }

            if(head >= s.length() || tail > s.length()){
                break;
            }

            stack.push(s.substring(head, tail));
            head = tail;
        }

        String reversed = new String();
        while (!stack.isEmpty()){
            reversed += stack.pop();
            reversed += " ";
        }
        if(reversed.length() > 0){
            reversed = reversed.substring(0, reversed.length() -1);
        }

        return reversed;
    }
}

1. str.substring(int, int); 而不是str.subString(int, int);
2. substring(int beginIndex,int endIndex)从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 "smiles".substring(1, 5) returns "mile"
3. 在塞完所有单词到stack里后,最后一个个拿出来打印的时候,切记切记循环体for()里面不可以是int i = 0; i < stack.size(); i++!,因为你for{}里面的操作有在pop,那你等于size这个值就一直在变!可以

int wordCount = stack.size();
if(!stack.isEmpty()){
  for(int i = 0; i < wordCount - 1; i++){
    reversed+= stack.pop();
    reversed+= " ";
  }
  reversed += stack.pop();
}

或者

while (!stack.isEmpty()){
            reversed += stack.pop();
            reversed += " ";
        }
        if(reversed.length() > 0){
            reversed = reversed.substring(0, reversed.length() -1);
        }}

4.注意关注输入处理,null, "", " "。在心里跑一下他们

时间: 2024-08-10 23:17:04

lintcode53 Reverse Words in a String -easy的相关文章

Lintcode53 Reverse Words in a String solution 题解

[题目描述] Given an input string, reverse the string word by word. 给定一个字符串,逐个翻转字符串中的每个单词. [题目链接] http://www.lintcode.com/en/problem/reverse-words-in-a-string/ [题目解析] 这道题让我们翻转字符串中的单词,题目中给了我们写特别说明,如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),所以我们只能对

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

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:

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