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):

 public class Solution {
public String reverseWords(String s) {
String result = "";
int i = 0;
int j = s.length();
for(i = s.length()-1; i >= 0; i--)
{
if(s.charAt(i) == ' ')
{
if(i == s.length()-1)
{
j = i;
//continue;
}
else if(i == 0)
{
//i++;
break;
}
else if(j == i+1)
{
j = i;
//continue;
}
else
{
result = result.concat(s.substring(i+1,j)+" ");
j = i;
}
}
}
result = result.concat(s.substring(i+1,j));
if(!result.isEmpty() && result.charAt(result.length()-1) == ' ')
result = result.substring(0,result.length()-1);
return result;
}
}

使代码更加精简,改为:

MyAnswer 2(Java):

 public class Solution {
public String reverseWords(String s) { String result = "";
int j = s.length();
int i = j-1; for(; i >= 0; i--)
{
if(s.charAt(i) == ' ')
{
if(i == 0)
{
break;
}
else if(i != s.length()-1 && j != i+1)
{
result = result.concat(s.substring(i+1,j)+" ");
}
j = i;
}
} result = result.concat(s.substring(i+1,j)); int k = result.length();
if(!result.isEmpty() && result.charAt(k-1) == ' ')
result = result.substring(0,k-1);
return result;
}
}

MyAnswer3 (C++):

 class Solution {
public:
void reverseWords(string &s) {
string result = "";
int j = ;
int i;
for(i = s.length()-; i >= ; i--)
{
if(s[i] == ' ')
{
if(j == )
continue;
result = result + s.substr(i+, j) + " ";
j = ;
continue;
}
j++;
}
result = result + s.substr(i+, j);
s = (result[result.length()-] == ' ')?result.substr(,result.length()-):result;
}
};

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

  1. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  2. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  3. [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 ...

  4. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  7. 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 arithm ...

  8. leetcode6 Reverse Words in a String 单词取反

    Reverse Words in a String  单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...

  9. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

随机推荐

  1. NLP国内研究方向机构导师

    基础研究 词法与句法分析:李正华.陈文亮.张民(苏州大学) 语义分析:周国栋.李军辉(苏州大学) 篇章分析:王厚峰.李素建(北京大学) 语言认知模型:王少楠,宗成庆(中科院自动化研究所) 语言表示与深 ...

  2. C语言union关键字,union和struct区别

    union 关键字的用法与struct 的用法非常类似. union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在union 中所有的数据成员共用一个空间,同一时间 ...

  3. 数据库实例: STOREBOOK > 表空间

    ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 表空间(默认) 1. 表空间(默认)返回顶部 1.1, 1.2, 2. 表空间列表(默认)返回顶部 2.1, SYSA ...

  4. C++中对Mysql的操作函数可以参考以下blog中的内容

    http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html

  5. 第一章 HttpClient的使用

    1.http协议(这一块儿有时间的话会做记录) 2.常用的两种RPC方式 基于http协议:HttpClient和JDK自己的Http操作类 基于TCP或UDP协议:mina2和netty(这一部分以 ...

  6. WebSettings 文档 API 翻译 常用设置

    . setDefaultFontSize(int size)  Sets the default font size. The default is 16. setDefaultTextEncodin ...

  7. 在Android中使用Android Ksoap2调用WebService

    一.WebService介绍 WebService是基于SOAP协议可实现web服务器与web服务器之间的通信,因采用SOAP协议传送XML数据具有平台无关性,也是成为解决异构平台之间通信的重要解决方 ...

  8. Systemd 三部曲 之 PHP7

    安装编译php7时需要的依赖包 : yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-deve ...

  9. (转)Unity中武器与人物的碰撞检测

    自:http://blog.csdn.net/Monzart7an/article/details/24435843 目前来说有三种思路,其实前两种算变种了: 1.动画关键帧回调 + 范围检测. 这个 ...

  10. jQuery cssHook的经典例子

      /*--------------------------- example ------------------------------*/ $.cssHooks.foo = { get: fun ...