Advertisement
LikeRampage

C++/C#/Python leetcode leetcode 344. Reverse String

Nov 15th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | Source Code | 0 0
  1. C++ Code:
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. class Solution {
  6. public:
  7.     void reverseString(std::vector<char>& s) {
  8.         int left = 0;
  9.         int right = s.size() - 1;
  10.  
  11.         while (left < right) {
  12.             // Swap the characters at the left and right indices
  13.             std::swap(s[left], s[right]);
  14.             // Move towards the middle
  15.             left++;
  16.             right--;
  17.         }
  18.     }
  19. };
  20.  
  21. int main() {
  22.     Solution solution;
  23.  
  24.     // Example usage
  25.     std::vector<char> s = {'h', 'e', 'l', 'l', 'o'};
  26.     solution.reverseString(s);
  27.  
  28.     // Print the reversed string
  29.     std::cout << "Reversed string: ";
  30.     for (char c : s) {
  31.         std::cout << c;
  32.     }
  33.     std::cout << std::endl;
  34.  
  35.     return 0;
  36. }
  37. Explanation:
  38. Function: The reverseString method takes a reference to a vector of characters (std::vector<char>& s), which represents the string to be reversed.
  39. Two-Pointer Technique:
  40. Two pointers, left (starting from the beginning) and right (starting from the end), are used to swap characters.
  41. The characters at the left and right indices are swapped, and then the pointers are moved towards the center.
  42. Loop: The while loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
  43. Example Usage: In the main function, we create a vector of characters, call the reverseString function, and then print the reversed string.
  44. Example Output:
  45. For the input {'h', 'e', 'l', 'l', 'o'}, the output will be:
  46.  
  47. Reversed string: olleh
  48. Feel free to modify the input vector in the main function to test with different strings!
  49. C# Code:
  50. using System;
  51.  
  52. public class Solution {
  53.     public void ReverseString(char[] s) {
  54.         int left = 0;
  55.         int right = s.Length - 1;
  56.  
  57.         while (left < right) {
  58.             // Swap the characters at the left and right indices
  59.             char temp = s[left];
  60.             s[left] = s[right];
  61.             s[right] = temp;
  62.  
  63.             // Move towards the middle
  64.             left++;
  65.             right--;
  66.         }
  67.     }
  68. }
  69.  
  70. public class Program {
  71.     public static void Main() {
  72.         Solution solution = new Solution();
  73.  
  74.         // Example usage
  75.         char[] s = { 'h', 'e', 'l', 'l', 'o' };
  76.         solution.ReverseString(s);
  77.  
  78.         // Print the reversed string
  79.         Console.WriteLine("Reversed string: " + new string(s));
  80.     }
  81. }
  82. Explanation:
  83. Function Signature: The ReverseString method takes a character array (char[] s) as input, which represents the string to be reversed.
  84. Two-Pointer Technique:
  85. Two pointers, left and right, are initialized to the start and end of the array, respectively.
  86. The characters at these two pointers are swapped, and then the pointers are moved towards each other.
  87. Loop: The loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
  88. Example Usage: In the Main method, we create a character array, call the ReverseString method, and then print the reversed string using Console.WriteLine.
  89. Example Output:
  90. For the input {'h', 'e', 'l', 'l', 'o'}, the output will be:
  91.  
  92. Reversed string: olleh
  93. You can modify the input character array in the Main method to test with different strings.
  94. 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.
  95.  
  96. Python Code:
  97. class Solution:
  98.     def reverseString(self, s: list[str]) -> None:
  99.         left, right = 0, len(s) - 1
  100.        
  101.         while left < right:
  102.             # Swap the characters at the left and right indices
  103.             s[left], s[right] = s[right], s[left]
  104.             # Move towards the middle
  105.             left += 1
  106.             right -= 1
  107.  
  108. # Example usage
  109. if __name__ == "__main__":
  110.     solution = Solution()
  111.    
  112.     # Example input
  113.     s = ['h', 'e', 'l', 'l', 'o']
  114.     solution.reverseString(s)
  115.    
  116.     # Print the reversed string
  117.     print("Reversed string:", s)
  118. Explanation:
  119. Function: The reverseString method takes a list of characters (s: list[str]) and reverses it in place.
  120. Two-Pointer Technique:
  121. Two pointers, left (starting from the beginning) and right (starting from the end), are used to swap characters.
  122. The characters at the left and right indices are swapped, and then the pointers are moved towards the center.
  123. Loop: The while loop continues until the left pointer is no longer less than the right pointer, ensuring that all characters are swapped.
  124. Example Usage: In the if __name__ == "__main__": block, we create a list of characters, call the reverseString method, and then print the reversed list.
  125. Example Output:
  126. For the input ['h', 'e', 'l', 'l', 'o'], the output will be:
  127.  
  128. Reversed string: ['o', 'l', 'l', 'e', 'h']
  129. You can modify the input list in the example usage to test with different strings!
Tags: C++ C# python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement