Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C++ Code:
- #include <iostream>
- #include <vector>
- class Solution {
- public:
- void reverseString(std::vector<char>& s) {
- int left = 0;
- int right = s.size() - 1;
- while (left < right) {
- // Swap the characters at the left and right indices
- std::swap(s[left], s[right]);
- // Move towards the middle
- left++;
- right--;
- }
- }
- };
- int main() {
- Solution solution;
- // Example usage
- std::vector<char> s = {'h', 'e', 'l', 'l', 'o'};
- solution.reverseString(s);
- // Print the reversed string
- std::cout << "Reversed string: ";
- for (char c : s) {
- std::cout << c;
- }
- std::cout << std::endl;
- return 0;
- }
- Explanation:
- Function: The reverseString method takes a reference to a vector of characters (std::vector<char>& s), which represents the string to be reversed.
- Two-Pointer Technique:
- Two pointers, left (starting from the beginning) and right (starting from the end), are used to swap characters.
- The characters at the left and right indices are swapped, and then the pointers are moved towards the center.
- Loop: The while loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
- Example Usage: In the main function, we create a vector of characters, call the reverseString function, and then print the reversed string.
- Example Output:
- For the input {'h', 'e', 'l', 'l', 'o'}, the output will be:
- Reversed string: olleh
- Feel free to modify the input vector in the main function to test with different strings!
- C# Code:
- using System;
- public class Solution {
- public void ReverseString(char[] s) {
- int left = 0;
- int right = s.Length - 1;
- while (left < right) {
- // Swap the characters at the left and right indices
- char temp = s[left];
- s[left] = s[right];
- s[right] = temp;
- // Move towards the middle
- left++;
- right--;
- }
- }
- }
- public class Program {
- public static void Main() {
- Solution solution = new Solution();
- // Example usage
- char[] s = { 'h', 'e', 'l', 'l', 'o' };
- solution.ReverseString(s);
- // Print the reversed string
- Console.WriteLine("Reversed string: " + new string(s));
- }
- }
- Explanation:
- Function Signature: The ReverseString method takes a character array (char[] s) as input, which represents the string to be reversed.
- Two-Pointer Technique:
- Two pointers, left and right, are initialized to the start and end of the array, respectively.
- The characters at these two pointers are swapped, and then the pointers are moved towards each other.
- Loop: The loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
- Example Usage: In the Main method, we create a character array, call the ReverseString method, and then print the reversed string using Console.WriteLine.
- Example Output:
- For the input {'h', 'e', 'l', 'l', 'o'}, the output will be:
- Reversed string: olleh
- You can modify the input character array in the Main method to test with different strings.
- Sure! Below is a Python solution for the LeetCode problem 344, "Reverse String". This solution reverses a given character array (list of characters) in place.
- Python Code:
- class Solution:
- def reverseString(self, s: list[str]) -> None:
- left, right = 0, len(s) - 1
- while left < right:
- # Swap the characters at the left and right indices
- s[left], s[right] = s[right], s[left]
- # Move towards the middle
- left += 1
- right -= 1
- # Example usage
- if __name__ == "__main__":
- solution = Solution()
- # Example input
- s = ['h', 'e', 'l', 'l', 'o']
- solution.reverseString(s)
- # Print the reversed string
- print("Reversed string:", s)
- Explanation:
- Function: The reverseString method takes a list of characters (s: list[str]) and reverses it in place.
- Two-Pointer Technique:
- Two pointers, left (starting from the beginning) and right (starting from the end), are used to swap characters.
- The characters at the left and right indices are swapped, and then the pointers are moved towards the center.
- Loop: The while loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
- Example Usage: In the if __name__ == "__main__": block, we create a list of characters, call the reverseString method, and then print the reversed list.
- Example Output:
- For the input ['h', 'e', 'l', 'l', 'o'], the output will be:
- Reversed string: ['o', 'l', 'l', 'e', 'h']
- You can modify the input list in the example usage to test with different strings!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement