摘要:公眾號(hào)愛寫給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。示例輸入輸出注意在字符串中,每個(gè)單詞由單個(gè)空格分隔,并且字符串中不會(huì)有任何額外的空格。
公眾號(hào):愛寫bug(ID:icodebugs)
給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。
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.
示例 1:
輸入: "Let"s take LeetCode contest" 輸出: "s"teL ekat edoCteeL tsetnoc"
注意:在字符串中,每個(gè)單詞由單個(gè)空格分隔,并且字符串中不會(huì)有任何額外的空格。
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
解題思路:? 每次遇到空格字符,就把 從上一次空格字符開始到該空格字符止之間的所有字符反轉(zhuǎn)一下即可,只需要注意最后一個(gè)字符結(jié)束時(shí),并不是空格字符,要再加一個(gè)判斷是否是已經(jīng)索引到最后一位。
"abc def" 原字符串 ["a" , "b" , "c" , " " , "d" , "e" ,"f"] 轉(zhuǎn)成char[]型數(shù)組 ["c" , "b" , "a" , " "...] 遍歷數(shù)組,遇到第一個(gè)空格,把該空格到上個(gè)空格之間的字母反轉(zhuǎn) [... " " , "d" , "e" ,"f"] 遍歷到最后一位,不是空格,依然要反轉(zhuǎn)到前一個(gè)空格間的字母 [... " " , "f" , "d" ,"e"] 反轉(zhuǎn) "cba fde" 轉(zhuǎn)成字符串輸出Java:
class Solution { public String reverseWords(String s) { int sLen = s.length(), k = 0, j = 0;//j記錄空格字符前的索引位置 char strs[] = s.toCharArray(), temp;//轉(zhuǎn)為字符數(shù)組 for (int i = 0; i < sLen; i++) { if (strs[i] == " ") j = i - 1;//遇到空格字符j值減1,為截取的字母段的最后一個(gè)字母的索引 else if (i == sLen - 1) j = i;//如果到最后一位,則j值不應(yīng)該再減1 else continue; for (; j >= k; j--, k++) {//交換位置 temp = strs[j]; strs[j] = strs[k]; strs[k] = temp; } k = i + 1;//k記錄空格字符后的索引位置 } return String.valueOf(strs); } }
python不再?gòu)?fù)現(xiàn)上述定義指針解題的思路,這里再次投機(jī)取巧,利用 python 切片特性及 split() 、join() 函數(shù)解題,解題思路:
"abc def gh" 原字符串 "hg fed cba" 切片特性反轉(zhuǎn)字符串 ["hg" , "fed" , "cba"] split()分割字符串 ["cba" , "fed" , "hg"] 切片反轉(zhuǎn)數(shù)組 "cba fed hg" 拼接成字符串Python3:
class Solution: def reverseWords(self, s: str) -> str: return " ".join(s[::-1].split()[::-1])
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/45138.html
摘要:公眾號(hào)愛寫給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。示例輸入輸出注意在字符串中,每個(gè)單詞由單個(gè)空格分隔,并且字符串中不會(huì)有任何額外的空格。 公眾號(hào):愛寫bug(ID:icodebugs) 給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。 Given a string, you need to revers...
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...
摘要:題目鏈接題目分析題目要求把句子中的每個(gè)單詞都倒轉(zhuǎn)過(guò)來(lái)。思路這個(gè)很簡(jiǎn)單,用空格把句子分割,再用把字符串倒轉(zhuǎn)過(guò)來(lái),拼接起來(lái)就可以了。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 557. Reverse Words in a String III 題目鏈接 557. Reverse Words in a String III 題目分析 題目要求把句子中的每個(gè)單詞都倒轉(zhuǎn)過(guò)來(lái)。 思路 這個(gè)...
摘要:一題目描述空格分隔,逐個(gè)反轉(zhuǎn)二題目描述三題目描述當(dāng)然也可以用的做,不過(guò)用雙指針更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 題目描述 Given a string, you need to reverse the order of chara...
摘要:第題反轉(zhuǎn)字符串中的單詞給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。示例輸入輸出注意在字符串中,每個(gè)單詞由單個(gè)空格分隔,并且字符串中不會(huì)有任何額外的空格。 LeetCode 第557題 557. 反轉(zhuǎn)字符串中的單詞 III 給定一個(gè)字符串,你需要反轉(zhuǎn)字符串中每個(gè)單詞的字符順序,同時(shí)仍保留空格和單詞的初始順序。 示例 1: 輸入: Lets tak...
閱讀 3437·2021-11-22 09:34
閱讀 2991·2021-10-09 09:43
閱讀 1525·2021-09-24 09:47
閱讀 2260·2019-08-30 12:53
閱讀 1063·2019-08-29 14:00
閱讀 3495·2019-08-29 13:17
閱讀 2338·2019-08-28 18:00
閱讀 1352·2019-08-26 12:00