题目:

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. 使用传入的总记录数实现一条sql语句完成分页查询

    使用传入的总记录数实现一条sql语句完成分页查询     问题:在传统的分页查询的实现中不可避免的需要两条sql语句,一条用于查询数据一条用于查询总记录数.如下面的实际代码所示: Img1 当然如果使 ...

  2. Page_Load接收随机参数放到字典类中

    Page_Load接收随机参数放到字典类中,可以用作签名.普通的接收url的参数可以用作下面这种模式:  int appid =Convert.ToInt32(param["appid&qu ...

  3. 使用CLR Profiler分析.NET程序

    使用CLR Profiler分析.NET程序 就像剥去.NET语法糖衣的工具(Reflector等)很多一样,我们可以用来分析.NET程序性能的工具有很多,如前面一片博文DebugLZQ给大家介绍的v ...

  4. 用aspx文件作为模板

    用aspx文件作为模板 首先申明:标题中的如果是可以去掉的. 想写这篇文章很长时间了,一来是跟大家分享一下,别浪费时间在写代码生成器上面了,什么CodeSmith,XXCodeGenerator等等, ...

  5. [解决]Windows Server 2012 不能安装IE版的Flash

    1.问题描述 在server 2012下安装IE版的Flash提示包含最新版本 2.解决方法 2.1.开启“桌面体验”功能 2.2.关闭“IE增强的安全配置”功能 3.重启计算机后安装Flash

  6. Python之路 1Day

    1.python基础入门   作者:黄旭 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始,比如 >>> ...

  7. 设计模式之 - 工厂方法模式 (Factory Method design pattern)

    1. 模式意图:  定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 2. 别名(Virtual Constructor) 3. 结构 4. 工厂方法模式C ...

  8. 克隆虚拟机win8系统后注意修改安全标识(SID)

    克隆虚拟机win8系统后注意修改安全标识(SID)   克隆虚拟机系统后两个系统硬件配置一样,需要注意修改:security ID ,MAC,计算机名,IP地址,产品激活 重置工具:sysprep.e ...

  9. 微信小程序开源项目库汇总

    最近做了一个微信小程序开源项目库汇总,里面集合了OpenDigg 上的优质的微信小程序开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. UI组件 ...

  10. Java重写与重载之间的区别

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...