leetcode151. 翻转字符串里的单词
生活随笔
收集整理的這篇文章主要介紹了
leetcode151. 翻转字符串里的单词
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
給定一個字符串,逐個翻轉(zhuǎn)字符串中的每個單詞。
示例 1:
輸入: “the sky is blue”
輸出: “blue is sky the”
代碼
class Solution {public String reverseWords(String s) {int n=s.length(),i=0;ArrayList<String> arrayList=new ArrayList<>();while (i<n)//提取每個單詞{if(s.charAt(i)==' '){i++;}else{StringBuilder stringBuilder=new StringBuilder();while (i<n&&s.charAt(i)!=' '){stringBuilder.append(s.charAt(i));i++;}arrayList.add(stringBuilder.toString());}}Collections.reverse(arrayList);//逆序return String.join(" ",arrayList);//加空格} }總結(jié)
以上是生活随笔為你收集整理的leetcode151. 翻转字符串里的单词的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode1177. 构建回文串检
- 下一篇: leetcode468. 验证IP地址