码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 151 Reverse Words in a String

时间:2015-06-05 11:50:13      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

解题思路:

本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下:

    public String reverseWords(String s) {
		if (s == null || s.length() == 0)
			return s;
		String[] str = s.split(" ");
		StringBuilder sb = new StringBuilder();
		for (int i = str.length - 1; i >= 0; i--)
			if (str[i].length() >= 1)
				sb.append(str[i] + " ");
		if (sb.length() > 1)
			sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
    }

 

Java for LeetCode 151 Reverse Words in a String

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4553964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!