LintCode- 翻转字符串
来源:互联网 发布:农行mac版 编辑:程序博客网 时间:2024/06/05 11:24
翻转字符串
给定一个字符串,逐个翻转字符串中的每个单词。
样例
给出s = “the sky is blue”,返回”blue is sky the”
说明
单词的构成:无空格字母构成一个单词
输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个
//方法一public class Solution { /** * @param s : A string * @return : A string */ public String reverseWords(String s) { ArrayList<String> arr = new ArrayList<String>(); if(s==""){ return ""; } for(int i = 0; i<s.length();i++ ){ while(i<s.length()&&s.charAt(i)==' '){ i++; } int a = i; while(i<s.length()&&s.charAt(i)!=' '){ i++; } int b = i; if(i==s.length()-1){ arr.add(s.substring(a)); break; } arr.add(s.substring(a, b)); } String re = ""; Collections.reverse(arr); for(String x : arr){ re = re + " "+x; } return re.substring(1).trim(); }}//方法二public class Solution { /** * @param s : A string * @return : A string */ public String reverseWords(String s) { String [] a = s.split(" "); String re =""; for(int i =a.length-1;i>-1;i--){ if(i==0){ re = re+a[i]; }else{ re = re + a[i] + " "; } } return re; }}
0 0
- LintCode- 翻转字符串
- lintcode --翻转字符串
- LintCode(easy)翻转字符串
- LintCode(3):翻转字符串
- lintcode,翻转字符串
- Lintcode 翻转字符串
- LintCode 53 翻转字符串
- lintcode--翻转字符串
- [LintCode]53.翻转字符串
- lintcode翻转字符串
- [Lintcode] #53 翻转字符串
- 翻转字符串(LintCode)
- LintCode-剑指Offer-(53)翻转字符串
- LintCode 第53题 翻转字符串
- 【LintCode】Reverse Words in a String 翻转字符串
- 【LintCode】Reverse Words in a String 翻转字符串
- LintCode 翻转二叉树
- LintCode-翻转链表
- 我的iOS学习历程 - UIView和UILabel
- 编写方法,完成指定文件的复制和剪切;使用BufferedReader和BufferedWriter完成。
- hdu1159 Common Subsequence (最长公共子序列)
- 关于兔子问题(斐波那契数)的Java实现--采用面向对象的方式
- muduo库阅读(22)——文件压缩类
- LintCode- 翻转字符串
- python 爬虫及multiprocessing包——多线程爬取并解析百度贴吧某贴小trick
- 常见问题时间复杂度(1)
- Android NDK 如何缩减库的大小
- Linux数据库进程?
- 2012 Asia Tianjin Regional Contest - locker 暴力DP
- 线性表(二)——单链表(3)——单链表应用举例(1)—— 拆分单链表
- java jni 入门4 - 访问实例域,静态域
- ubuntu14更新使用清华的源,163的太失望了