Question Link:
https://leetcode.com/problems/reverse-string/
Question Description
Reverse String in place
Input: s = [“h”,”e”,”l”,”l”,”o”]
Output: [“o”,”l”,”l”,”e”,”h”]
Simulation
Solution 1: Use two pointers to swap values.
Iteration 1: [“h”,”e”,”l”,”l”,”o”] -> [“o”,”e”,”l”,”l”,”h”]
Iteration 2: [“o”,”e”,”l”,”l”,”h”] -> [“o”,”I”,”l”,”e”,”h”]
Iteration 3: [“o”,”I”,”l”,”e”,”h”] -> [“o”,”I”,”l”,”e”,”h”]
Implementation
public void reverseString(char[] s) {int left = 0, right = s.length - 1;char temp;while (left <= right) {temp = s[left];s[left] = s[right];s[right] = temp;right--;left++;}}
