题目:

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.

The input string does not contain leading or trailing spaces and the words are always separated by a single space.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Could you do it in-place without allocating extra space?

分析:

这题应用的原理是两次求逆等于原String的原则, 第一次将整体String全部求逆, 然后利用单词之间的空格, 再分开求逆,最后实现将整个sentence单词完全倒序的结果. 比较巧妙

代码:

public class Solution {
    public void reverseWords(char[] s) {
      reverse(s, 0, s.length);
      for (int i = 0, j = 0; j <= s.length; j++){
        if (j == s.length || s[j] == ' ') {
          reverse(s, i , j);
          i = j + 1;
        }
      }
    }

    private void reverse(char[] s, int start, int end) {
       for (int i = 0; i < (end - start) / 2; i++) {
          char temp = s[start + i];
          s[start + i] = s[end - i - 1];
          s[end - i - 1] = temp;
       }
    }
}

Leetcode - 186 Reverse Words in a String II的更多相关文章

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

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  2. leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  3. 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...

  4. 186. 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-s ...

  5. 186. Reverse Words in a String II 翻转有空格的单词串 里面不变

    [抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...

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

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

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

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

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

  9. LeetCode Reverse Words in a String II

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

随机推荐

  1. ubuntu 学习笔记1--安装jdk

    ubuntu 版本13.10 安装jdk参考网址http://stackoverflow.com/questions/14788345/how-to-install-jdk-on-ubuntulinu ...

  2. 采用SOLR进行全文索引的完整解决方案,设计图

  3. WPF4.5新特性(MSDN的翻译读不太懂)

    WPF4.5新特性(MSDN的翻译读不太懂) 1. 新的Doctype声明 XHTML的声明太长了,我相信很少会有前端开发人员能手写出这个Doctype声明. <!DOCTYPE html PU ...

  4. 谱聚类(Spectral Clustering)详解

    谱聚类(Spectral Clustering)详解 谱聚类(Spectral Clustering, SC)是一种基于图论的聚类方法——将带权无向图划分为两个或两个以上的最优子图,使子图内部尽量相似 ...

  5. Kendo UI开发教程(9): Kendo UI Validator 概述

    Kendo UI Validator 支持了客户端校验的便捷方法,它基于HTML 5 的表单校验功能,支持很多内置的校验规则,同时也提供了自定义规则的便捷方法. 完整的Kendo UI 的Valida ...

  6. HDU 2073 无限的路

    Problem Description 甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形: 甜甜的好朋友蜜蜜发现上面的图还 ...

  7. fastboot,bootloader,recovery

    Fastboot模式,在这个模式下,我们可以为手机进行解锁.可以在该模式下连接电脑进行软件或者指令解锁(s-off),这个模式也称“下载模式” Fastboot fastboot是一种比recover ...

  8. 关闭Windows 2008下面应用程序出错后的提示

    写了一个服务器端程序,没有能处理所有的错误,总有一些错误会抛出到系统中去.于是写了一个进程守护者,一旦发现服务器端退出,可以在第一时间重新启动服务器,也算是一种折中的方案吧.理论上讲应该是可行的,但是 ...

  9. 方法object面试题分析:7JAVA中Object的clone方法详解-克隆-深克隆

    时间紧张,先记一笔,后续优化与完善.     每日一道理 翻开早已发黄的页张,试着寻找过去所留下的点点滴滴的足迹.多年前的好友似乎现在看来已变得陌生,匆忙之间,让这维持了多年的友谊变淡,找不出什么亲切 ...

  10. jvm加载class原理

    “委托机制”是指先委托父类装载器寻找目标类,只有在找不到的情况下才从自己的路径中查找并载入.这一点是从安全的方面考虑的,试想一下如果有人写了一个恶意的基础类(如java.lang.String)并加载 ...