Leetcode: Reverse Words in a String, leetcodereverse

Source: Internet
Author: User

Leetcode: Reverse Words in a String, leetcodereverse

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.

The direct idea of this question is to use the String split function to divide the String into substrings by space and store them in a String [] strs, in this case, the string in strs is reversed from large to small, and spaces are added in the middle. Note that if the original string contains multiple spaces, some of the substrings in our strs array are empty strings. If we encounter these empty strings during Reverse conversion, we should skip them. In time, the split operation is O (N), and the result is also O (N) after another scan ). A String [] and StringBuffer are used in the space, which is also O (N)

Note some syntaxes. For example, the parameter of the split function is String rather than Char. When I wrote it, I accidentally made a low-level error again. to judge whether it is a Null String, do not write it as if (str = "") or use equals (), either use length () = 0 to judge

 1 public class Solution { 2     public String reverseWords(String s) { 3         if (s==null || s.length()==0) return s; 4         s.trim(); 5         String[] strs = s.split(" "); 6         StringBuffer res = new StringBuffer(); 7         for (int i=strs.length-1; i>=0; i--) { 8             if (strs[i].length() == 0) continue; 9             res.append(strs[i]);10             res.append(' ');11         }12         return res.toString().trim();13     }14 }

 


C ++ returns a string? Why have I tested both dev c ++ and vc60? But cannot it be used in leetcode?

The Runtime Error does not mean that your program cannot be compiled, but that your program encountered an Error during running. This error is generally caused by out-of-bounds array access, stack overflow, and illegal memory access.

Runtime error occurs in multiply strings in leetcode.

Leetcode is positioned as an interview question, not an ACM question
That is to say, the question is not determined by AC or not whether your answer is correct or not.
What's more, give others a feeling during the interview.

This was originally a question about the string representing the large number and the uncle multiplication, but the po master used stringstream and vector
The answer in the interview won't leave a good impression on the interviewer even if the answer is correct.

I will help you debug the program, but I still think it is better to write a new one that is easy to understand and avoid using STL and stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.