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-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的原则, 第一次将整体String全部求逆, 然后利用单词之间的空格, 再分开求逆,最后实现将整个sentence单词完全倒序的结果. 比较巧妙
代码:
public class Solution {
public void reverseWords(char[] s) {
reverse(s, 0, s.length);
for (int i = 0, j = 0; j <= s.length; j++){
if (j == s.length || s[j] == ' ') {
reverse(s, i , j);
i = j + 1;
}
}
}
private void reverse(char[] s, int start, int end) {
for (int i = 0; i < (end - start) / 2; i++) {
char temp = s[start + i];
s[start + i] = s[end - i - 1];
s[end - i - 1] = temp;
}
}
}
Leetcode - 186 Reverse Words in a String II的更多相关文章
- [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 旋转字符数组 ---------- 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 解题报告 (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 ...
随机推荐
- gearman with postgresql as persistent Queuing
gearman is a good thing gearman client --------------> gearman server <----------------------- ...
- 关于《数据格式修改》的简单应用程序(C语言)
关于<数据格式修改>的简单应用程序(C语言) 至亲爱的博友: 大家好,好久不见了.由于博主还是一名大学在校生,不可避免的需要和指导教师共同完成一些项目,因此前一段时间暂时停止了博客的更新. ...
- struts2 ValueStack(值栈)解析
Struts2一个重要点就是值栈. ValueStack,是用来存储一些在各个action,或者说是通过s标签.el表达式等给前台Jsp等页面展示的东西. ValueStack是一个接口,其内部接口非 ...
- zookeeper删除kafka元数据
问题:卸载kafka前未删除kafka topic,重新安装kafka后,生成跟之前topic名字相同的topic时报错,显示topic已存在 [root@d96 ~]# kafka-topics - ...
- linux服务器开发二(系统编程)--线程相关
线程概念 什么是线程 LWP:Light Weight Process,轻量级的进程,本质仍是进程(在Linux环境下). 进程:独立地址空间,拥有PCB. 线程:也有PCB,但没有独立的地址空间(共 ...
- SQL注入(二)
5.限制输入长度 如果在Web页面上使用文本框收集用户输入的数据,使用文本框的MaxLength属性来限制用户输入过长的字符也是一个很好的方法,因为用户的输入不够长,也就减少了贴入大量脚本的可能性.程 ...
- 《Dynamic Topic Detection and Tracking: A Comparison of HDP, C-Word, and Cocitation Methods》笔记
原文地址:http://onlinelibrary.wiley.com/doi/10.1002/asi.23134/abstract 黄色背景是我认为比较重要的,红色字体是我自己的话. 动态主题监测与 ...
- Token注解防止表单的重复提交
注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...
- solr最佳实践
管理页面 页面地址:http://{ip}:{port}/solr/#/ 管理页面的data-import页可以手动重建索引,configuration指定了数据源,重建索引也可以通过http请求触发 ...
- JS函数调用
function SayHello(word) { console.log(word); } function execute(Somefunction,value) { Somefunc ...