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 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 --;
}
}
}
Reverse Words in a String II的更多相关文章
- Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...
- [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 ...
- [Swift]LeetCode186. 翻转字符串中的单词 II $ 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] 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 ...
- 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 旋转字符数组 ---------- java
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 ...
- 186. Reverse Words in a String II 翻转有空格的单词串 里面不变
[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...
随机推荐
- CF1111E Tree 树链剖分,DP
CF1111E Tree 过年了,洛咕还没爬这次的题,先放个CF的链接吧. 补个LG传送门. 对于每个询问点\(x\),设它的祖先即不能和它放在同一个集合中的点的个数为\(f[x]\),设\(dp[i ...
- Java动态代理:一个面包店的动态代理帝国
文章首发于[博客园-陈树义],点击跳转到原文大白话说Java动态代理:一个面包店的动态代理帝国 代理模式是设计模式中非常重要的一种类型,而设计模式又是编程中非常重要的知识点,特别是在业务系统的重构中, ...
- python 中的特殊方法,纠正自己笨笨的记忆
1. __new__ 和 __init__ 的区别 python 2.x 老式类(默认继承type) class A: pass 老式类中没有__new__类方法(也就是说定义也不会执行,它不是老式类 ...
- [PLC]ST语言五:STL/RET/CMP/ZCP
一:STL/RET/CMP/ZCP 说明:简单的顺控指令不做其他说明. 控制要求:无 编程梯形图: 结构化编程ST语言: (*步进指令STL(EN,s);*) SET(M8002,S3); STL(T ...
- CTF MISC-USB流量分析出题记录
USB流量分析 USB接口是目前最为通用的外设接口之一,通过监听该接口的流量,可以得到很多有意思的东西,例如键盘击键,鼠标移动与点击,存储设备的明文传输通信.USB无线网卡网络传输内容等. 1.USB ...
- Python之NMAP详解
一.NMAP简介 NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连 ...
- MAC node + git + bower 简单安装
一 node 安装 打开https://nodejs.org/en/ nodejs官网 下载安装文件 双击.pkg 文件 自动安装即可 二 安装git 打开 http://code.google.co ...
- Influxdb配置文件详解---influxdb.conf
官方介绍:https://docs.influxdata.com/influxdb/v1.2/administration/config/ 全局配置 1 2 reporting-disabled = ...
- GlusterFS分布式存储集群-1. 部署
参考文档: Quick Start Guide:http://gluster.readthedocs.io/en/latest/Quick-Start-Guide/Quickstart/ Instal ...
- python正则表达式,以及应用[下载图片]
regular expresion由一系列特定字符及其组合成的字符串,用来对目标字符串进行过滤操作.. re相关知识点 python正则表达式库为re,用import re导入,在然后用re.comp ...