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. idea安装Lombok及使用介绍

    原文:https://blog.csdn.net/motui/article/details/79012846 Lombok使用 介绍 在项目中使用Lombok可以减少很多重复代码的书写.比如说get ...

  2. Shell获取文件的文件名和扩展名的例子

    这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下 basename example.tar.gz .tar.gz # => examp ...

  3. KJBitmap与KJHttp的深度用法

    摘要 本文原创,转载请注明地址:http://kymjs.com/code/2015/09/24/01这篇文章是对KJFrameForAndroid使用的一个深入学习 之前写过一些有关KJFrame的 ...

  4. Android 限制EditText仅仅能输入数字、限制输入类型、限制输入长度的小技巧

    准确的说让Edittext仅仅能输入数字有方法两种,都是通过xml属性设置 方法一: <EditText android:id="@+id/u_account" androi ...

  5. windows下androidNDK环境配置

    一:什么是NDK? NDK 提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so 和java 应用一起打包成apk.这些工具对开发者的帮助是巨大的. NDK 集成了交叉编译器, ...

  6. 如何下载android源码与android内核源码

    首先,要分清楚,android的源代码和android的内核代码一般是分开的,要分别进行下载. 1.先下载android的源代码.(这里不包括android的内核代码)      下载最新的源代码,一 ...

  7. Android之从Browser中打开本地的应用程序&微信检测是否有对应app

    在对应的应用程序的AndroidManifest.xml中配置: <activity android:name=".ui.TabHostActivity" android:w ...

  8. 文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write

    文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write close(关闭文件) 相关函数 ope ...

  9. C# 指南

    C# 指南提供了许多有关 C# 语言的资源. 此网站面向许多不同的受众群体. 你可能希望探索本指南的不同部分,具体视你的编程经验或 C# 语言和 .NET 使用经验而定. 对于从未接触过编程的开发者: ...

  10. 牛气冲天的Iframe应用笔记

    纵观时下网站,本来网速就有些慢,可是几乎每页都要放什么Banner,栏目图片,版权等一大堆雷同的东西,当然,出于网站风格统一.广告效应的需要,本无可厚非,可毕竟让用户的钱包为这些“点缀“的东西”日益消 ...