设为首页 加入收藏

TOP

Reverse Words in a String
2015-07-20 17:46:55 来源: 作者: 【 】 浏览:2
Tags:Reverse Words String

问题描述

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

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

解决方案

public class Solution {
	public String reverseWords(String s) {
		String[] words = s.split( " " );
		if( words.length == 0 ) {
			return "";
		}
		StringBuilder result = new StringBuilder( words[words.length - 1] );
		int index = words.length - 1;
		while( --index >= 0 ) {
			if( !words[index].equals( "" ) ) {
				result.append( " " ).append( words[index] );
			}
		}
		return result.toString();
	}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1867 A + B for you again KM.. 下一篇Hdu 1106 排序 (atoi函数与 strt..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)
·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)