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. 两台装有Ubuntu系统的服务器搭建VPN(一台为本地服务器,另一台为云服务器)

    我们搭建VPN采用的是openvpn,搭建过程总体需要经过三大步骤:   1.openvpn的安装与配置    2.端口转发    3.系统重启iptables规则自动生效  注意:以下所有名令在ro ...

  2. c调用python

    #include <Python.h>//python33(python2.x有几个函数不对应) /* PyImport_ImportModule 导入一个Python模块并返回它的指针 ...

  3. 关于WebView的复习

    最近不知为啥,公司提出要用webView,偷笑不止.在青软上课的时候学过这东西,是说条件比较紧张的时候可以拿来充数的,现在公司里手机端开发人员很多,做的好好的,放着原生ui不用,非要赶时髦搞什么Web ...

  4. STM32——assert_param(expr)

    在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用.如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义 ...

  5. Psp个人软件开发软件需求分析和用例分析

    Psp个人软件开发软件需求分析和用例分析 一.需求分析 1.业务需求 1.1 应用背景 开发项目进度计划总是那么不明确,延期经常出现,甚至无法给出一个相对比较明确的延迟时间.这样给市场的推广会带来很大 ...

  6. shell 指定范围随机数抽取

  7. 【zz】matlab 腐蚀膨胀算法

    1.图像膨胀的Matlab实现: 可以使用imdilate函数进行图像膨胀,imdilate函数需要两个基本输入参数,即待处理的输入图像和结构元素对象.结构元素对象可以是strel函数返回的对象,也可 ...

  8. 理解python的with语句

    Python’s with statement provides a very convenient way of dealing with the situation where you have ...

  9. 关于zero_interconnect_delay_mode和nonzero_interconnect_delay_mode的区别

    在执行report_qor命令时,需要注意这样一个问题,对于Reg_2_Reg路径,在worst scene情况下,nonzero模型要比zero模型报出的结果差,但在best scene情况下,却要 ...

  10. linux命令:df

    1.命令介绍: df用来检测磁盘空间占用情况. 2.命令格式: df [选项] 文件 3.命令参数: 必要参数: -a 全部文件系统列表 -h 方便阅读方式显示 -H 等于“-h”,但是计算式,1K= ...