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. 平时常用的Visual Studio操作技巧,持续更新中……

    移除未使用的命名空间--方法1:右键--"组织using"--"移除未使用的using"--方法2:Shift+F10--"O"-" ...

  2. ExtJS创建选项卡

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Unity中关于Device Filter的选择问题

    引言 目前工作的Unity版本是5.4.1f,发布Android版本.apk的时候,对包体的大小有些疑问,就上网查了下资料,发现Build Settings——Player Settings——Oth ...

  4. 将 MRC 项目转换为 ARC 项目

    其实很简单,直接使用系统工具转换即可,以下是步骤: 1. 你之前的项目是MRC的 2. 里面有 release, dealloc 等. 3. 开始转化 4. 点击save转化完后,记得把工程重新设置成 ...

  5. ubuntu MySQL安装和设置

    1. apt-get install mysql-server 2. apt-get isntall mysql-client 修改 /etc/mysql/my.cnf #bind-address   ...

  6. Top N之MapReduce程序加强版Enhanced MapReduce for Top N items

    In the last post we saw how to write a MapReduce program for finding the top-n items of a dataset. T ...

  7. Java IO 体系结构

    参考文章地址: http://blog.csdn.net/oracle_microsoft/article/details/2634231 Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚 ...

  8. 【Cocos2d-X开发学习笔记】第12期:动作类CCAction的详细讲解

    一般对于游戏中的精灵而言,它们不仅仅是存在于场景中,而且是动态展现的,例如,精灵移动的动态效果.动 画效果.跳动效果.闪烁和旋转动态效果等.每一种效果都可以看成是精灵的一个动作. 一.动作类(CCAc ...

  9. Vulkan --vulkan in powervr

    zhankeng 跨平台 多线程 low cpu overhead object orientated vulkan有利于tile based的地方 明确依赖声明 细粒度同步 render passe ...

  10. 关于VS 工具箱灰色,不可用的解决方案

    使用vs的命令行工具,在命令行中运行:devenv /ResetSkipPkgs ,重新打开vs,重置一下工具箱 ,OK,成功了~! 希望能对大家有帮助!