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?

1、刚开始的想法是找到第一个和最后一个单词,然后交换他们两个,但是遇到的问题是两个单词长度不一致的时候会很麻烦,就需要将剩余的字符串整个进行移动,麻烦,且效率很低。

2、参考了discuss,发现很其实想法很简单,就是分两步:第一步就是把整个字符串倒过来,第二步就是再翻回来之后再把每个单词反转一次就好了。

public class Solution {
public void reverseWords(char[] s) {
int len = s.length;
reverse(s, 0, len - 1);
int start = 0;
for (int i = 0; i < len - 1; i++){
if (s[i] == ' '){
reverse(s, start, i - 1);
start = i + 1;
}
}
reverse(s, start, len - 1);
}
private void reverse(char[] s, int start, int end){
while (start < end){
char ch = s[start];
s[start] = s[end];
s[end] = ch;
start++;
end--;
}
}
}

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java的更多相关文章

  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

    题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...

  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. php类的__get和__set方法

    (1)这两个方法是自动调用的 (2)这两个方法只有在成员变量是private的时候才会自己调用 testclass.php <?php class testclass { private $va ...

  2. 对于.NET Socket连接的细节记录

    如果客户端直接连接一个不存在的服务器端,客户端会抛出异常: 如果在连接过程中,客户端强制关闭了连接(没有调用Close直接关闭了程序),服务器端会抛出异常: 如果在连接过程中,客户端调用了Close, ...

  3. SharePoint Framework 概述

    博客地址:http://blog.csdn.net/FoxDave 本文翻译自新出的SharePoint Framework概述介绍文章,原文地址:http://dev.office.com/sh ...

  4. freeCAD文档结构

    一个freecad文档包含了你场景中的所有物体.它可以包含组及任何工作平台制造的物体.你可以切换工作台,但是它仍然工作在同一个文档上.当您保存您的工作时,该文件就被保存到磁盘上.你可以同时打开多个fr ...

  5. Linux安装FTP服务

    转自:http://blog.csdn.net/huangbiao86/article/details/6641952 ftp服务器 1.  在Linux和其他机器之间共享文件(在linux下安装ft ...

  6. 2014年6月份第3周51Aspx源码发布详情

      基于知识树的多课程网络教学平台源码  2014-6-16 [VS2008]功能介绍:本平台是一个支持网上教学的网站,支持多个课程,教师可根据需要创建课程,进行课程结构.题库等的管理.   技术特色 ...

  7. 北大poj-2688

    Cleaning Robot Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4395   Accepted: 1763 De ...

  8. iOS中修改头部tabBarButton 默认按钮的颜色和默认字体颜色

    +(void)initialize { //初始化设置主题 UINavigationBar *navBar = [UINavigationBar appearance]; [navBar setBac ...

  9. window.onload() 和 $(function(){})

    再使用 $(function(){})的时候,发现一直取不到元素.但是换成window.onload()则可以取到. 大概推测是页面加载问题,于是把js从header移到了footer,发现 $(fu ...

  10. WPF项目中所遇到的一些问题集

    1. 没有Timer控件 解决方案: 第一步:申明一个DispatcherTimer 类的变量, private DispatcherTimer timer; //定时控件 第二步:初始化这个类 ti ...