亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

Reverse Words in a String

keelii / 1121人閱讀

摘要:思路把以空格為間隔分隔開(kāi)存入然后倒著加入并且每個(gè)加入以后后面加空格,最后記的清除最后一個(gè)空格。

Reverse Words in a String

Given an input string, reverse the string word by word.

For example, Given s = "the sky is blue", return "blue is sky the".

思路: 把string以空格為間隔分隔開(kāi)存入array, 然后倒著加入stringBuilder并且每個(gè)加入以后后面加空格,最后記的清除最后一個(gè)空格。

public class Solution {
   public String reverseWords(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }

        String[] array = s.split(" ");
        StringBuilder sb = new StringBuilder();

        for (int i = array.length - 1; i >= 0; i--) {
            if (!array[i].equals("")) {
                sb.append(array[i]).append(" ");
            }
        }

        //remove the last " "
        return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64797.html

相關(guān)文章

  • [Leetcode] Reverse Words in a String 反轉(zhuǎn)單詞順序

    摘要:代碼先反轉(zhuǎn)整個(gè)數(shù)組反轉(zhuǎn)每個(gè)單詞雙指針交換法復(fù)雜度時(shí)間空間思路這題就是版的做法了,先反轉(zhuǎn)整個(gè)數(shù)組,再對(duì)每個(gè)詞反轉(zhuǎn)。 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = the sky is blue, return blue is...

    codeKK 評(píng)論0 收藏0
  • [LeetCode] Reverse Words in a String II

    Problem Reverse Words in a String IIGiven an input string , reverse the string word by word. Example Input: [t,h,e, ,s,k,y, ,i,s, ,b,l,u,e]Output: [b,l,u,e, ,i,s, ,s,k,y, ,t,h,e] Note A word is defin...

    浠ラ箍 評(píng)論0 收藏0
  • [LeetCode] 557. Reverse Words in a String III

    Problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1:Input: Lets take LeetCode contest...

    104828720 評(píng)論0 收藏0
  • Leetcode PHP題解--D20 557. Reverse Words in a String

    摘要:題目鏈接題目分析題目要求把句子中的每個(gè)單詞都倒轉(zhuǎn)過(guò)來(lái)。思路這個(gè)很簡(jiǎn)單,用空格把句子分割,再用把字符串倒轉(zhuǎn)過(guò)來(lái),拼接起來(lái)就可以了。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 557. Reverse Words in a String III 題目鏈接 557. Reverse Words in a String III 題目分析 題目要求把句子中的每個(gè)單詞都倒轉(zhuǎn)過(guò)來(lái)。 思路 這個(gè)...

    LoftySoul 評(píng)論0 收藏0
  • leetcode 151. Reverse Words in a String

    摘要:題目要求講一個(gè)字符串中的單詞逆序輸出,單詞中字母順序不發(fā)生改變。其中,字符串首位的空格應(yīng)刪去,字符串中如果存在多余的空格,只需輸出一個(gè)空格。這里用到的正則表達(dá)式為也就是遇到一個(gè)或多個(gè)空白時(shí)斷句。 題目要求 Given an input string, reverse the string word by word. For example, Given s = the sky is ...

    yzd 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<