在加拿大
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 没仔细看,可能运行不了。算伪代码吧, ·加拿大新闻 加拿大9月份房屋销售年比增长5%,CREA上调预测,房市年底将表现
·加拿大新闻 安省怒了:重犯想保释?没门!三振出局!联邦也准备动真格的
·加拿大新闻 翻车!大温华人业主嫌地税太高!瞎编案例,求降房产估值$90
·加拿大新闻 奥迪的豪赌:中国豪华车最后的赢家?
·加拿大新闻 特斯拉ModelYL又多了个选项
·汽车 旧车最亮的合法车灯?
·汽车 发动机支架品牌推荐