📜  反转 [L, R] 范围内的给定字符串

📅  最后修改于: 2022-05-13 01:57:07.587000             🧑  作者: Mango

反转 [L, R] 范围内的给定字符串

给定一个字符串str和两个整数LR ,任务是反转[L, R]范围内的字符串,即str[L…R]
例子:

方法:

  1. 如果范围无效,即L < 0R ≥ lenL > R ,则打印原始字符串。
  2. 如果范围有效,则在L < R时继续交换字符str[L]str[R] ,并在每次交换操作后更新L = L + 1R = R – 1 。最后打印更新的字符串。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the string after
// reversing characters in the range [L, R]
string reverse(string str, int len, int l, int r)
{
 
    // Invalid range
    if (l < 0 || r >= len || l > r)
        return str;
 
    // While there are characters to swap
    while (l < r) {
 
        // Swap(str[l], str[r])
        char c = str[l];
        str[l] = str[r];
        str[r] = c;
 
        l++;
        r--;
    }
 
    return str;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
    int l = 5, r = 7;
 
    cout << reverse(str, len, l, r);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
    // Function to return the string after
    // reversing characters in the range [L, R]
    static String reverse(char[] str, int len,
                               int l, int r)
    {
 
        // Invalid range
        if (l < 0 || r >= len || l > r)
            return "Invalid range!";
 
        // While there are characters to swap
        while (l < r)
        {
 
            // Swap(str[l], str[r])
            char c = str[l];
            str[l] = str[r];
            str[r] = c;
 
            l++;
            r--;
        }
        String string = new String(str);
        return string;
    }
 
    // Driver code
    public static void main (String[] args)
    {
        String str = "geeksforgeeks";
        int len = str.length();
        int l = 5, r = 7;
 
        System.out.println(reverse(str.toCharArray(),
                                         len, l, r));
    }
}
 
// This code is contributed by Ashutosh450


Python3
# Python3 implementation of the approach
 
# Function to return the string after
# reversing characters in the range [L, R]
def reverse(string, length, l, r) :
 
    # Invalid range
    if (l < 0 or r >= length or l > r) :
        return string;
         
    string = list(string)
     
    # While there are characters to swap
    while (l < r) :
 
        # Swap(str[l], str[r])
        c = string[l];
        string[l] = string[r];
        string[r] = c;
 
        l += 1;
        r -= 1;
 
    return "".join(string);
 
# Driver code
if __name__ == "__main__" :
 
    string = "geeksforgeeks";
    length = len(string);
    l = 5; r = 7;
 
    print(reverse(string, length, l, r));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
     
    // Function to return the string after
    // reversing characters in the range [L, R]
    static String reverse(char[] str, int len,
                          int l, int r)
    {
 
        // Invalid range
        if (l < 0 || r >= len || l > r)
            return "Invalid range!";
 
        // While there are characters to swap
        while (l < r)
        {
 
            // Swap(str[l], str[r])
            char c = str[l];
            str[l] = str[r];
            str[r] = c;
 
            l++;
            r--;
        }
        return String.Join("",str);
    }
 
    // Driver code
    public static void Main (String[] args)
    {
        String str = "geeksforgeeks";
        int len = str.Length;
        int l = 5, r = 7;
 
        Console.WriteLine(reverse(str.ToCharArray(),
                                        len, l, r));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
geeksrofgeeks