LeetCode 186. Reverse Words in a String II(反转单词)
来源:互联网 发布:node导出excel 编辑:程序博客网 时间:2024/05/16 11:09
原题网址:https://leetcode.com/problems/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?
方法:两次反转。
public class Solution { private void reverse(char[] s, int from, int to) { while (from<to) { char c = s[from]; s[from] = s[to]; s[to] = c; from ++; to --; } } public void reverseWords(char[] s) { int pos = 0; for(int i=0; i<=s.length; i++) { if (i<s.length && s[i] != ' ') continue; reverse(s, pos, i-1); pos = i+1; } reverse(s, 0, s.length-1); }}
0 0
- LeetCode 186. Reverse Words in a String II(反转单词)
- LeetCode 151. Reverse Words in a String(反转单词)
- [LeetCode]Reverse Words in a String-反转单词
- [Leetcode] reverse words in a string 反转单词
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- 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
- Reverse Words in a String 字符串中单词反转
- Reverse Words in a String 反转单词顺序
- [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 解题报告
- leetcode Reverse Words in a String III 反转字符串中的单词
- 151. Reverse Words in a String & 186.Reverse Words in a String II & 翻转单词顺序列
- Http协议原理解析第一篇
- servlet监听器
- 第107课: Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL)
- Android NDK、cygwin安装及通过示例导出so库
- 2016年5月16日--5月22日(每天1小时,共7小时,剩2977小时)
- LeetCode 186. Reverse Words in a String II(反转单词)
- bzoj4567: [Scoi2016]背单词
- Android面试必考基础题
- LeetCode 187. Repeated DNA Sequences(重复DNA序列)
- Stream常用操作min,reduce
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- NLP相关
- 勾股定理每日一证 2
- Stream对数字处理