Q2:Reverse Words in a String

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.

MyAnswer 1 (Java):

 1 public class Solution {
 2     public String reverseWords(String s) {
 3         String result = "";
 4         int i = 0;
 5         int j = s.length();
 6         for(i = s.length()-1; i >= 0; i--)
 7         {
 8             if(s.charAt(i) == ‘ ‘)
 9             {
10                 if(i == s.length()-1)
11                 {
12                     j = i;
13                     //continue;
14                 }
15                 else if(i == 0)
16                 {
17                     //i++;
18                     break;
19                 }
20                 else if(j == i+1)
21                 {
22                     j = i;
23                     //continue;
24                 }
25                 else
26                 {
27                     result = result.concat(s.substring(i+1,j)+" ");
28                     j = i;
29                 }
30             }
31         }
32         result = result.concat(s.substring(i+1,j));
33         if(!result.isEmpty() && result.charAt(result.length()-1) == ‘ ‘)
34             result = result.substring(0,result.length()-1);
35         return result;
36     }
37 }

使代码更加精简,改为:

MyAnswer 2(Java):

 1 public class Solution {
 2     public String reverseWords(String s) {
 3
 4         String result = "";
 5         int j = s.length();
 6         int i = j-1;
 7
 8         for(; i >= 0; i--)
 9         {
10             if(s.charAt(i) == ‘ ‘)
11             {
12                 if(i == 0)
13                 {
14                     break;
15                 }
16                 else if(i != s.length()-1 && j != i+1)
17                 {
18                     result = result.concat(s.substring(i+1,j)+" ");
19                 }
20                 j = i;
21             }
22         }
23
24         result = result.concat(s.substring(i+1,j));
25
26         int k = result.length();
27         if(!result.isEmpty() && result.charAt(k-1) == ‘ ‘)
28             result = result.substring(0,k-1);
29         return result;
30     }
31 }
时间: 2024-11-03 22:16:25

Q2:Reverse Words in a String的相关文章

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

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

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候