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 ...
随机推荐
- CC.NET-自动化发布时 Web.config 文件维护
[Hello CC.NET]自动化发布时 Web.config 文件维护 在 <[Hello CC.NET]CC.NET 实现自动化集成> 的 HellowWorld 中经实现: 1. ...
- 使用diff和patch指令生成文件差异和还原文件
使用diff和patch指令生成文件差异和还原文件 创建一个新的文件夹test然后进入test mkdir test cd test 首先创建文件a vim a.txt 随便输入一段文字后保存不退出. ...
- siverlight 后台动态设置图片路径的总结
最近碰到了个问题,需要给一个用户控件中的image动态设置图片资源 1.图片资源属性为resource时,静态引用无任何问题,但是动态设置时,就什么也不显示 后来找到问题所在, 必须把此图片属性项中“ ...
- C++中#和##的特殊使用
1.用#号将输入的内容转换为字符串. 用##号将两个参数合并. #include <iostream> using namespace std; //将输入的内容转换成字符串 #defin ...
- es6 Module
前言: 这是阮一峰老师的ECMA6入门module一章的缩减,只抽取了我在项目中有用到的内容.带着问题去看老师的教程.感觉吸收更快,也明白了偶尔遇到的export不出来的问题. es6模块设计思想: ...
- 怎样求FIRST集、FOLLOW集和SELECT集
一,要知道什么是终结符和非终结符. 终结符:通俗的说就是不能单独出现在推导式左边的符号,也就是说终结符不能再进行推导. 非终结符:不是终结符的都是非终结符.(非男即女,呵呵) 如:A-->B,则 ...
- HTML5之Audio音频标签学习
HTML5中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. control ...
- mac 切换php版本
通过brew安装的php可以通过brew link和brew unlink来切换不同版本 #brew list #brew unlink php56 #brew link php55
- CodeForces 631D Messenger
$KMP$. $n=1$和$n=2$的时候可以单独计算.$n>2$时,可以拿字符和数字分别做一次匹配,然后扫描一遍判断一下就可以计算出答案了. #pragma comment(linker, & ...
- Linux环境快速部署Zookeeper集群
一.部署前准备: 1.下载ZooKeeper的安装包: http://zookeeper.apache.org/releases.html 我下载的版本是zookeeper-3.4.9. 2.将下载的 ...