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. MVC单元测试,使用Repository模式、Ninject、Moq

    本篇使用Repository设计MVC项目,使用Ninject作为DI容器,借助Moq进行单元测试. 模型和EF上下文 模型很简单: public class Foo { public int Id ...

  2. 转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发

    活动指示器(UIActivityIndicatorView)可以告知用户有一个操作正在进行中.进度指示器(UIProgressView )也具有同样功能,而且还可以告知用户离操作结束还多远. 这两个指 ...

  3. Arcgis Runtime for andriod 100 Simple marker symbol

    GraphicsOverlay graphicsOverlay = new GraphicsOverlay(); 58 mMapView.getGraphicsOverlays().add(graph ...

  4. 企业应用:C/S 开发需要考虑的事项

    备注 几乎没有做过 C/S 方面的开发(有 RIA 方面的开发经验),此文纯属个人胡思乱想,写下来是希望朋友们多给点意见. C/S 开发注意事项 C/S 开发需要注意如下几点: 采用何种模式组织 UI ...

  5. numpy转换

    csv2npy cccsv=numpy.genfromtxt('/root/c.csv', delimiter = ',') buf2npy imga=numpy.frombuffer(buf,num ...

  6. C++常用排序法、随机数

    C++常用排序法研究 2008-12-25 14:38 首先介绍一个计算时间差的函数,它在<time.h>头文件中定义,于是我们只需这样定义2个变量,再相减就可以计算时间差了. 函数开头加 ...

  7. C语言:将结构体数组的成绩按照从小到大进行排序。

    #include<stdio.h> typedef struct student { char *name; int sno; int age; float score; }Student ...

  8. eclipse svn 配置

    1.安装svn http://subversion.apache.org/ 到apache网站上下载svn 2.在eclipse中安装svn插件 在eclipse->help->eclip ...

  9. 第四章 四种List实现类的对比总结

    1.ArrayList 非线程安全 基于对象数组 get(int index)不需要遍历数组,速度快: iterator()方法中调用了get(int index),所以速度也快 set(int in ...

  10. crtmpserver实现防盗流和流推送验证

    Protecting your streams from webpage copy&paste flash code, listing or recording 保护流,防止在页面上被复制&a ...