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 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的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- 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"," ...
- [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& ...
- [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 ...
- [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 ...
随机推荐
- Python学习基本
刚开始学习是看了这个网站: 廖雪峰的官方网站 http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92707 ...
- EL表达式的使用
在JSP页面中嵌入了一部分Java代码,本以为Java代码中声明的变量可以通过${var}获得变量值输出,结果没有任何输出, EL表达式是用在对对象值.属性制的访问 ${user.id}.reques ...
- php 文件操作
$fn="e:\debug.txt"; if(is_writable($fn)==false){ die("不能写入"); } file_put_content ...
- 神盾解密工具 之 解密 “ PHP 神盾解密工具 ”
不知道某君删了我的评论,还是发在这里面. 其实对神盾解密并没有那么感兴趣,只是看到了博主把工具又加密了,感觉不爽.研究了一下,其实解密没那么复杂. 利用php_apd扩展很轻松地就这把这搞定了.只有四 ...
- matlab初学之plot颜色和线型
文章出处: http://jiangshuxia.9.blog.163.com/blog/static/3487586020116711375339/ 字母 颜色 标点 ...
- jsp提交表单数据乱码,内置对象,以及过滤器
jsp提交表单数据乱码解决方案 通过form表单给服务器提交数据的时候,如果提交的是中文数据,那么可能会出现乱码,如果表单的请求方式是post请求,那么可以使用如下方案解决乱码: 在调用getPara ...
- list排序
今天要对List排序,上网查了很多方法都感觉比较麻烦,现在终于找到了两种比较简便的方式,在此写出来,防止忘记!同时供大家参考! using System; using System.Collectio ...
- SSH中,使用Filter拦截直接访问JSP页面!
话不多说,直接上代码 创建一个Filter类 package com.weibo.util; import java.io.IOException; import javax.servlet.Filt ...
- 1.6jdk + eclipse + pydev搭建Python开发环境
直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...
- xlwt写入中文操作不成功,提示UnicodeDecodeError: ascii codec can't decode byte ...
打开xlwt包里的Workbook.py文件,修改Workbook类的__init__方法 将 def __init__(self, encoding='ascii', style_compressi ...