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 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的更多相关文章
- [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 ... 
- 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 ... 
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ... 
- 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 ... 
- 186. Reverse Words in a String II 翻转有空格的单词串 里面不变
		[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ... 
- [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& ... 
- [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 ... 
- [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 ... 
- LeetCode Reverse Words in a String II
		原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ... 
随机推荐
- php   常用数组操作
			php常用的数组操作函数,包括数组的赋值.拆分.合并.计算.添加.删除.查询.判断.排序等 array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 <?p ... 
- svn图标不显示的解决方案
			最近发现svn图标莫名其妙的不显示,其他操作都正常.在网上搜了一些方法. 解决方法一(失败): 升级最新版本,我的本来就是最新版本 解决方法二(失败): 右键->TortoiseSVN-> ... 
- Puppet安装及部署
			本篇博客主要介绍Puppet的安装部署,后续会更新其他相关内容 一.简介 二.环境介绍 三.安装Puppet 四.配置Puppet-dashboard 五.配置Puppet Kick 一.简介 Pup ... 
- Python开发入门与实战16-APACHE部署
			16. Windows平台apache部署 本章节我们简要的描述一下如何在windows平台部署apache的django站点. Python Django 项目部署发布到windows apache ... 
- Yosemite系统怎么录制 iOS8设备屏幕
			我一年前一直想要的一个功能,发布时很想用.一直没找到 ,很巧的是今天被测试发现了. 感谢CCTV.自己在这里也记录下: 你好! 在 OS X Yosemite 系统中,QuickTime 支持 ... 
- android:ToolBar详解
			android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ... 
- ie11浏览器和chrome浏览器对于bgsound和background的一些区别
			今天在编写一个非常简单的网页的时候,按照书上写的,使用了一个jpg图片作为背景图片,用background属性放在<body>标签内,同时使用<bgsound>标签插入背景音乐 ... 
- elasticsearch,python包pyes进行的处理
			elasticsearch:高性能搜索引擎,官网:https://www.elastic.co/products/elasticsearch/ 对于它相信大家都不陌生,es的使用已经广泛存在 各大网站 ... 
- freemarker string= null
			在java代码中经常会出现以下代码: String name; …………………… if(null == name || name.length == 0){ return; } 这行代码用freema ... 
- Apache Server Status主机状态查看
			10月30日,国外安全研究人员发现由于对apache设置不严,导致服务器状态暴露于公网.本来apache有一个叫server-status 的功能,为方便管理员检查服务器运行状态的.它是一个HTML页 ... 
