Hugo Future Imperfect Slim

Blackdiz's Garage

心得、筆記、雜記

1 分鐘

LeetCode 344. Reverse String

Easy

題目

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"]

解決方法1 (My Own Solution)

用兩個指標 startend 分別由陣列第一個和最後一個元素開始交換位置,並且彼此慢慢靠近,因為字串長度可能為奇數或偶數,所以直到 start >= end 為止 (當為奇數時 start == end,當為偶數時兩者會交錯所以用 start > end 判斷)。

Java Code
class Solution {
    public void reverseString(char[] s) {
        int end = s.length - 1;
        for (int start = 0; start < s.length; start++) {
              if (start >= end) {
                  return;
              }
              char temp = s[start];
              s[start] = s[end];
              s[end] = temp;
              end--;
        }
    }
}

如果有什麼想法或需要指正的地方,歡迎您留言或來信 😄

說些甚麼

留言

最新文章

分類

關於

I'm so weak, so I learn from every master for things that I don't know