LeetCode_001:Reverse Words in a String
2014-07-10 20:44
204 查看
package com.abuge; /** * 需求: * 输入一个字符串,将其反转。 * 例: * s = "the sky is blue" * return "blue is sky the" * 方法&步骤: * 1、 将字符串首尾空格去除 * 2、利用正则表达式,将字符串转换成字符串数组 * 3、反转字符串数组 * @author AbuGe * */ public class Solution { public static String reverseWords(String s) { String strTmp = s.trim(); String[] strArray = strTmp.split(" +"); //反转数组 int len = strArray.length; int startIndex = 0; int endIndex = len - 1; while(startIndex < endIndex) { String tmp = strArray[startIndex]; strArray[startIndex] = strArray[endIndex]; strArray[endIndex] = tmp; startIndex++; endIndex--; } //定义一个字符串缓冲区 StringBuffer sb = new StringBuffer(); for(int i = 0; i < len; i++) { sb.append(strArray[i]); if(i != len - 1) sb.append(" "); } return sb.toString(); } public static void main(String[] args) { String str = " the sky is blue "; String out = reverseWords(str); System.out.println(out); } }
相关文章推荐
- Leetcode 第151题 最新题解 Reverse Words in a String
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- LeetCode(151)Reverse Words in a String
- Leetcode: Reverse Words in a String
- LeetCode Reverse Words in a String
- 【LeetCode with Python】 Reverse Words in a String
- [LeetCode]Reverse Words in a String
- leetcode Reverse Words in a String
- [leetcode]Reverse Words in a String
- Leetcode_Reverse-Words-in-a-String
- LeetCode | Reverse Words in a String
- Reverse Words in a String (LeetCode)
- [Leetcode Solution] Reverse Words in a String
- LeetCode151:Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- [leetCode]Reverse Words in a String
- leetcode - Reverse Words in a String
- [LeetCode] Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 【LeetCode】Reverse Words in a String