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. SYS.AUD$无法扩容导致无法登录的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/bisal/article/details/19068663 昨天同事说有个测试库无法登录了,用PLS ...

  2. android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)

      android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创) 一直想搞NDK开发却一直给其他事情耽搁了,参考了些网上的资料今天终于把 ...

  3. 【甘道夫】通过Mahout构建推荐系统--通过IDRescorer扩展评分规则

    通过Mahout构建推荐系统时,假设我们须要添�某些过滤规则(比方:item的创建时间在一年以内),则须要用到IDRescorer接口,该接口源代码例如以下: package org.apache.m ...

  4. 测试 Nginx 作为前端下各种模式的性能

    测试环境: 1:Nginx 独立处理静态面页请求 5000,开了60个线程 2:Nginx作为前端请求转给 Weblogic 12c 处理 (Spring 4.0平台下的动态面页效果如图) 3:Ngi ...

  5. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  6. 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: UNDOTBS1

    ylbtech-Oracle:数据库实例: STOREBOOK  >  表空间  >  编辑 表空间: UNDOTBS1 表空间  >  编辑 表空间: UNDOTBS1 1. 一般 ...

  7. 关于MFC框架程序中CWinApp::OnIdle

    很早之前就发现,我写的图形引擎在MFC框架程序中的刷帧率始终在60FPS左右.好在自己的程序对刷帧率的要求不是很高,所以一直没有太过纠结此事.直到今天看了别人的程序才发现应该在函数CWinApp::O ...

  8. CURL库在C++程序中的运用浅析

    最近由于要做一个爬虫项目,要对很多网站进行爬取,所以一直都在看这方面的文章.在翻阅了很多资料后,下载了一个curl库,着实对项目有了很大的帮助. 一.LibCurl基本编程框架 二.一些基本的函数 三 ...

  9. ftp 命令行操作 经常使用命令

    > ftp <host> [port] > pwd  # 查看当前文件夹 > dir  # 查看FTPserver中的文件及文件夹 > mkdir <dirn ...

  10. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...