Reverse Words in a String II
2024-10-01 11:12:47
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 --;
}
}
}
最新文章
- 对CSS中的Position、Float属性的一些深入探讨
- 引用类型(object、array)
- Divide and conquer:4 Values whose Sum is 0(POJ 2785)
- 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)
- IPC——匿名管道
- 学习ajax 总结
- log4j详细配置说明
- linux下Rtree的安装
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
- POJ 2538 WERTYU水的问题
- 【Xilinx-Petalinux学习】-03-PetaLinux通过eMMC方式启动
- ORACLE中关于外键缺少索引的探讨和总结
- 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】
- Linux内存描述之概述--Linux内存管理(一)
- Selenium2自动化测试实战(基于Python语言)— 编写第一个自动化脚本
- [ActionScript 3.0] 加载子swf需要指定应用程序域
- 管理 python logging 日志使用
- 将Excel的数据导入DataGridView中(转)
- loj2537 「PKUWC2018」Minimax 【概率 + 线段树合并】
- 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)
热门文章
- 【HNOI2007】紧急疏散
- CentOS 7 安装 caffe
- Scrapy爬取携程桂林问答
- 自动化运维工具saltstack04 -- 之jinja模板
- [ 转]Shell中参数($0,$1,$#,$NF,$@等)的含义
- 用shell实现bat批处理的pause命令-追加改进
- python FTP服务器实现(Python3)
- 最新Python笔试题2017 涵盖知识面广泛
- servlet 和 threadlocal 与 web容器(理解threadlocal)
- Mac SpotLight无法搜索