在加拿大
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 没仔细看,可能运行不了。算伪代码吧, ·加拿大新闻 CRA花$1800万造聊天机器人66%答案竟然是错的!
·加拿大新闻 北约克一名男子凌晨遇袭 情况危殆
·加拿大新闻 奔驰小G谍照,或搭混动或燃油动力
·加拿大新闻 合资品牌电动化反攻战,能否重塑中国豪华电动车市场格局
·加拿大新闻 房东注意!短租不是想做就能!BC女律师业主手握“批准邮件”
·澳洲新闻 新南威尔士州州长承诺收紧枪支法,使其适合用途
·澳洲新闻 Sajid Akram 于 1998 年持学生签证来到澳大利亚,他的儿子于 2019 年