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 反转一遍。

 public class Solution {
public void reverseWords(char[] s) {
for(int i = 0, j = 0; j <= s.length && i < s.length; j ++){
if(j == s.length || s[j] == ' '){
reverse(s, i, j - 1);
i = j + 1;
}
}
reverse(s, 0, s.length - 1);
} private void reverse(char[] c, int s, int e){
while(s < e){
char tmp = c[s];
c[s] = c[e];
c[e] = tmp;
s ++; e --;
}
}
}

最新文章

  1. 对CSS中的Position、Float属性的一些深入探讨
  2. 引用类型(object、array)
  3. Divide and conquer:4 Values whose Sum is 0(POJ 2785)
  4. 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)
  5. IPC——匿名管道
  6. 学习ajax 总结
  7. log4j详细配置说明
  8. linux下Rtree的安装
  9. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
  10. POJ 2538 WERTYU水的问题
  11. 【Xilinx-Petalinux学习】-03-PetaLinux通过eMMC方式启动
  12. ORACLE中关于外键缺少索引的探讨和总结
  13. 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】
  14. Linux内存描述之概述--Linux内存管理(一)
  15. Selenium2自动化测试实战(基于Python语言)— 编写第一个自动化脚本
  16. [ActionScript 3.0] 加载子swf需要指定应用程序域
  17. 管理 python logging 日志使用
  18. 将Excel的数据导入DataGridView中(转)
  19. loj2537 「PKUWC2018」Minimax 【概率 + 线段树合并】
  20. 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)

热门文章

  1. 【HNOI2007】紧急疏散
  2. CentOS 7 安装 caffe
  3. Scrapy爬取携程桂林问答
  4. 自动化运维工具saltstack04 -- 之jinja模板
  5. [ 转]Shell中参数($0,$1,$#,$NF,$@等)的含义
  6. 用shell实现bat批处理的pause命令-追加改进
  7. python FTP服务器实现(Python3)
  8. 最新Python笔试题2017 涵盖知识面广泛
  9. servlet 和 threadlocal 与 web容器(理解threadlocal)
  10. Mac SpotLight无法搜索