在加拿大
Given an input string, reverse the string word by word.ClarificationWhat constitutes a word?A sequence of non-space characters constitutes a word and some words have punctuation at the end.Could the input string contain leading or trailing spaces?Yes. However, your reversed string should not contain leading or trailing spaces.How about multiple spaces between two words?Reduce them to a single space in the reversed string.Example代码:Example 1: Input: "the sky is blue" Output: "blue is sky the" Explanation: return a reverse the string word by word.Example 2: Input: "hello world" Output: "word hello" Explanation: return a reverse the string word by word.-----------Java: public String reverseWords(String s) { if(s==null || s.equals("") || s.equals(" ")){return "";} String after = s.trim().replaceAll(" +", " "); String words[] = after.split(" "); String rst = ""; for (int i = words.length - 1; i >= 0 ; i --) { rst += words[i] + " "; } return rst; }大家觉得这解法怎么样?
评论
邪恶联盟 说:String rst = "";最好改成String rst =words[0];好处就是,如果不含空格,那就原样输出。点击展开...好像不对吧?
评论
gongbao 说:好像不对吧?点击展开...是的,没问题如果我写的话,就那样了(String rst =words[0]; )然后循环从1开始到结束。哈哈个人习惯问题(c++风格)Java: public String reverseWords(String s) { if(s==null || s.equals("") || s.equals(" ")){return "";} String after = s.trim().replaceAll(" +", " "); String words[] = after.split(" "); String rst = words[0]; for (int i = 1; i <=words.length - 1 ; ++i) { rst = words[i] + " " + rst; } System.out.println(rst); }}
评论
https://forum.iask.ca/threads/中共的宣传全是为了掩饰为什么说贸易战中共国必败.873382/ 赏 2019-04-08#4
绝圣弃智返璞归真
7,204 $0.00 没仔细看,可能运行不了。算伪代码吧, ·中文新闻 被取消测试揭幕战的内森·麦克斯威尼(Nathan McSweeney)在节礼日
·中文新闻 闷闷不乐的安德鲁王子对自己的失宠感到非常厌倦,以至于他“