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"," ...
随机推荐
- cogs791 [HAOI2012] 音量调节
大水题 这种题谁不能1A谁就吔屎/退役吧(说的就是我) 设dp[i][j]表示调完前i个音量为j有没有可能 没了 // It is made by XZZ // Fei Fan Ya Xi Lie~~ ...
- html元素可以同时实现两个class的功能
html元素标签可以放置两个不同的class文件并分别实现效果
- NGUI可展开列表的实现
本文来自网易云社区 作者:汪毅军 最近使用了NGUI做了下可展开列表,其主要思路如下:首先最外层使用Scroll view以达到滑动效果,然后列表使用UITable进行排列,最后通过点击Item控制I ...
- 推荐11个实用Python库
1.delorea 非常酷的日期/时间库 from delorean import Delorean EST = "US/Eastern"d = Delorean(timezone ...
- vue组件--通讯录
简介 在移动端开发中,通讯录是个很常见的需求. 通讯录通常要实现以下功能 首字母导航 滚动到一定位置首字母固定 在线通讯录demo 布局 通讯录是典型的上下两栏布局,上面是header,下面是内容区, ...
- 小佬頭眼里的读研VS工作
最近小佬頭在各种平台和论坛看到很多临近毕业的本科生在纠结读研和工作.于他们来说,人生到了一个十字路口,需要做出一个选择然后继续前行,今天小佬頭就来聊聊读研和工作的话题. 其实有这个考虑的同学不在少数, ...
- Netty源码分析第2章(NioEventLoop)---->第5节: 优化selector
Netty源码分析第二章: NioEventLoop 第五节: 优化selector 在剖析selector轮询之前, 我们先讲解一下selector的创建过程 回顾之前的小节, 在创建NioEv ...
- 基于openvswitch+Docker构建SDN网络测试环境 (使用ovs-docker进行构建)
这是一篇之前写的笔记,主要记录了使用openvswitch + Docker 等进行一个小型的SDN网络搭建的操作步骤.由于 之前临时有其他任务,耽搁了一下,最近开始重新整理,并计划开发一个简单的Py ...
- 打包一个传统的ASP.NET web app作为Docker镜像
(1)针对NerdDinner应用的Dockerfile内容如下 PS E:\DockeronWindows\Chapter02\ch02-nerd-dinner> cat .\Dockerfi ...
- C++判断回文
判断一个字符串是否为回文,如“goddog”. 代码: #include <iostream> #include <string> #include <stdio.h&g ...