close

因為這系列做起來很快,所以就索性一次貼在一篇文了!

PART 1-344. Reverse String (Easy)

不需要回傳一個 list,只需更改 s 即可!

同時,題目要求:in-place 的去做 swap

Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Example 2:

Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

image

image

更偷懶的!直接 call 內建 function!

image

 

PART 2 - 541. Reverse String II (Easy)

Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.

If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

題目看了霧煞煞,直接看範例

Example 1:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"

Example 2:

Input: s = "abcd", k = 2
Output: "bacd"

懂了嗎?就是要每 2k 個字元中,前 k 個字元做 reverse。

image

image

image

另外,也分享一些還不錯的網民做法:(遞迴)

image

 

PART 3 - 557. Reverse Words in a String III (Easy)

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Example 2:

Input: s = "God Ding"
Output: "doG gniD"

image

image

arrow
arrow
    創作者介紹
    創作者 Sofie 舒霏 的頭像
    Sofie 舒霏

    Sofie 舒霏的部落格

    Sofie 舒霏 發表在 痞客邦 留言(0) 人氣()