這題要實現字串反轉。
LeetCode 題目連結
https://leetcode.com/problems/reverse-string/
題目
Write a function that reverses a string. The input string is given as an array of characters
char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
Example 2:
Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]
Accept 作法
可以值得注意的是字串的記憶體位置需要一樣,不能另外創一個新的陣列去承接。Runtime: 117 ms
Memory: 44.6 MB
Java 程式碼
class Solution { public void reverseString(char[] s) { for(int i = 0; i < s.length/2;i++){ char tmp = s[i]; s[i] = s[s.length - 1 - i]; s[s.length-1 - i] = tmp; } } }
更多 LeetCode 相關資源
複習程式面試書籍
除了 LeetCode 練習外,我也入手了這本,題庫來自真正的面試,並非摘自教科書。它們反映出頂尖公司真正會出的題目,你可以藉此做好充分準備。
需要的話可以看看,寫得很仔細。
需要的話可以看看,寫得很仔細。
沒有留言:
張貼留言