题目:

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. javascript中的promise和deferred:实践(二)

    javascript中的promise和deferred:实践(二) 介绍: 在第一节呢,我花了大量的时间来介绍promises和deferreds的理论.现在呢,我们来看看jquery中的promi ...

  2. Python-数据库支持

    10.Python-数据库支持 使用数据库的好处: a.支持数据的并发访问,多个用户同时对基于磁盘的数据进行读写而不造成任何文件的损坏: b.支持根据多个数据字段或属性进行复杂的搜索: 1.如何操作数 ...

  3. Python 3语法小记(五)字符串

    Python 3 的源码的默认编码方式为 UTF-8 在Python 3,所有的字符串都是使用Unicode编码的字符序列. utf-8 是一种将字符编码成字节序列的方式.字节即字节,并非字符.字符在 ...

  4. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

  5. Swift 3.0 字符串、数组、字典的使用

    1.字符串 string func stringTest() -> Void { // 字符串 let str1 = "yiyi" let str2 = "2222 ...

  6. Python中的eval()、exec()及其相关函数

    刚好前些天有人提到eval()与exec()这两个函数,所以就翻了下Python的文档.这里就来简单说一下这两个函数以及与它们相关的几个函数,如globals().locals()和compile() ...

  7. 读书笔记:《HTML5开发手册》--现有元素的变化

    读书笔记:<HTML5开发手册>-- 现存元素的变化 继续学习HTML5语义化的内容,今天主要介绍一下,HTML5之前的元素经HTML5规范后的语义及一些使用示例. 一.cite HTML ...

  8. 搭建PHP建站环境

    PHP是一种网站后端脚本语言,通常在web开发中使用apache+PHP+MYSQL这种黄金搭档来建立支持PHP的站点,PHP运行环境或者说任何技术的运行环境都不是简单的加法,即使是安装有apache ...

  9. python3.4 data type

    #coding=utf-8 #Python 3.4 https://docs.python.org/3.4/library/ #IDE:Eclipse +PyDev Window10 import a ...

  10. JS 脚本应该放在页面哪个位置 head body foot

    我们平时在页面上写JS 是放在头部<head>中呢 还是放到body 最下面 能更优化? 查了一番资料,推荐 放在页面底部如: <html> <head> < ...