【Leetcode】Reverse Words in a String II
来源:互联网 发布:mysql 博客 编辑:程序博客网 时间:2024/06/01 09:53
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?
public class Solution { public void reverseWords(char[] s) { int len = s.length, i , j; reverse(s, 0, len-1); for(i=0,j=0;j<len;j++){ if(s[j]==' '){ reverse(s,i,j-1); i=j+1; } } reverse(s, i, len - 1); } public void reverse(char[] s, int start, int end){ for(int i=start, j=end; i< j; i++,j--){ char tmp = s[j]; s[j] = s[i]; s[i] = tmp; } }}
0 0
- leetcode Reverse Words in a String II
- leetcode Reverse Words in a String II
- [Leetcode]Reverse Words in a String II
- *LeetCode-Reverse Words in a String II
- 【Leetcode】Reverse Words in a String II
- [LeetCode] Reverse Words in a String II(in-place 算法)
- LeetCode LeetCode 186 - Reverse Words in a String II
- Leetcode NO.186 Reverse Words in a String II
- LeetCode - Reverse Words in a String I && II
- LeetCode 题解(262) : Reverse Words in a String II
- [leetcode] 186. Reverse Words in a String II 解题报告
- LeetCode 186.Reverse Words in a String II
- 【编程】【leetcode】186.Reverse Words in a String II
- [Leetcode] 186. Reverse Words in a String II 解题报告
- Reverse Words in a String II
- Reverse Words in a String II
- Reverse Words in a String II
- [LeetCode186]Reverse Words in a String II
- 提高你使用android studio的效率之键盘篇
- 2009年浙江大学计算机及软件工程研究生机试真题
- shader实例(二十)法线贴图实现凹凸效果
- 93. Restore IP Addresses
- Binary Tree Traversal二叉树遍历方法总结
- 【Leetcode】Reverse Words in a String II
- hdu 2521(反素数)
- 华为的JAVA面试题及答案(部分)
- 20070929迅雷面试部分题
- hello world
- 经典排序算法(Java版)
- HDU1298 T9
- JAVA面试题4
- 2016-03-31 书籍列表