Reverse Words
来源:互联网 发布:淘宝老顾客营销方案 编辑:程序博客网 时间:2024/06/05 00:20
Reverse Words
Write a function that reverses the order of the words in a string. For example, your function should transform the string “Do or do not, there is no try.” to “try. no is there not, do or Do”. Assume that all words are space delimited and treat punctuation the same as letters.
class Solution { public static String reverseWords(char[] str) { if (str == null || str.length == 0) return null; reverseStr(str, 0, str.length - 1); int start = 0; int end = 0; while (end < str.length) { if (str[end] != ' ') { start = end; while ( end < str.length && str[end] != ' ') end++; end--; reverseStr(str, start, end); } end++; } return new String(str); } private static void reverseStr(char[] str, int start, int end) { while (end > start) { char temp = str[start]; str[start] = str[end]; str[end] = temp; end--; start++; } } public static void main(String[] args) { String str = "Do or do not, there is no try."; char[] ch = str.toCharArray(); System.out.print(reverseWords(ch)); }}
0 0
- Reverse words
- Reverse Words
- Problem B. Reverse Words
- Leetcode - Reverse Words
- Reverse words in a string.
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- 【剑指Offer学习】【面试题60:把二叉树打印出多行】
- 黑马程序员_IO流体系(一)
- 修改 Ubuntu 桌面分辨率
- IOS开发 - 04.自定义视图
- 增强图文件
- Reverse Words
- IOS开发 - 05 通过Xib创建自定义视图
- POJ 3224 Go for Lab Cup!(水~)
- 【剑指Offer学习】【面试题61:按之字形顺序打印二叉树】
- node.js(一)
- Android开发工程师必备知识点(参考)
- 好用的图形工具yEd Graph Editor
- hdu 5280 Senior's Array
- scikit-learn:训练分类器、预测新数据、评价分类器