📜  反转数组,而无需在代码中的任何位置使用减号“-”

📅  最后修改于: 2021-05-05 02:54:37             🧑  作者: Mango

给定一个数组,任务是在不使用代码中任何地方的减号“-”的情况下反转数组。反转数组并不难,但最主要的是不要使用’-‘运算符。

在询问:Moonfrog采访

以下是不同的方法:

方法1:
1-在C++中将数组元素存储到向量中。
2-然后使用预定义功能反转向量。
3-然后将反向元素存储回数组中。

方法2:
1-将数组元素存储到堆栈中。
2-由于堆栈遵循后进先出的方式,因此我们可以存储来自
栈顶到数组中,数组本身将以相反的方式出现。

方法3:
1-在此方法中,其想法是使用负号,但将其存储到变量中。
2-通过使用此语句x =(INT_MIN / INT_MAX),我们在变量x中得到-1。
3-由于INT_MIN和INT_MAX具有相同的值,只是符号相反,因此在将它们相除时将得到-1。
4-然后,’x’可用于从最后开始递减索引。

C++
// C++ program to reverse an array without
// using "-" sign
#include 
using namespace std;
  
// Function to reverse array
void reverseArray(int arr[], int n)
{
    // Trick to assign -1 to a variable
    int x = (INT_MIN / INT_MAX);
  
    // Reverse array in simple manner
    for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        swap(arr[i], arr[n + (x * i) + x]);
}
  
// Drivers code
int main()
{
    int arr[] = { 5, 3, 7, 2, 1, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    reverseArray(arr, n);
  
    // print the reversed array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}


Java
// Java program to reverse an array without
// using "-" sign
class GFG {
  
    // Function to reverse array
    static void reverseArray(int arr[], int n)
    {
        // Trick to assign -1 to a variable
        int x = (Integer.MIN_VALUE / Integer.MAX_VALUE);
  
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
            // Swap ith index value with (n-i-1)th
            // index value
            swap(arr, i, n + (x * i) + x);
    }
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Drivers code
    public static void main(String[] args)
    {
        int arr[] = { 5, 3, 7, 2, 1, 6 };
        int n = arr.length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python program to reverse an array without
# using "-" sign
  
# Function to reverse array
def reverseArray(arr, n):
  
    import sys
  
    # Trick to assign - 1 to a variable
    x = -sys.maxsize // sys.maxsize
  
    # Reverse array in simple manner
    for i in range(n//2):
  
        # Swap ith index value with (n-i-1)th
        # index value
        arr[i], arr[n + (x*i) + x] = arr[n + (x*i) + x], arr[i]
  
  
# Driver code
if __name__ == "__main__":
    arr = [5, 3, 7, 2, 1, 6]
    n = len(arr)
  
    reverseArray(arr, n)
  
    # print the reversed array
    for i in range(n):
        print(arr[i], end=" ")
  
# This code is contributed by
# sanjeev2552


C#
// C# program to reverse an array without
// using "-" sign
using System;
  
class GFG {
  
    // Function to reverse array
    static void reverseArray(int[] arr, int n)
    {
        // Trick to assign -1 to a variable
        int x = (int.MinValue / int.MaxValue);
  
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
            // Swap ith index value with (n-i-1)th
            // index value
            swap(arr, i, n + (x * i) + x);
    }
  
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Drivers code
    public static void Main()
    {
        int[] arr = { 5, 3, 7, 2, 1, 6 };
        int n = arr.Length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP


C++
// C++ program to reverse an array without
// using "-" sign
#include 
using namespace std;
  
// Function to reverse array
void reverseArray(int arr[], int n)
{
  
    // Reverse array in simple manner
    for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        swap(arr[i], arr[(n + ~i + 1) + ~1 + 1]);
}
  
// Driver code
int main()
{
    int arr[] = { 5, 3, 7, 2, 1, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    reverseArray(arr, n);
  
    // print the reversed array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}


Java
// Java program to reverse an array without
// using "-" sign
import java.util.Arrays;
  
class GFG {
  
    // Function to reverse array
    static void reverseArray(int arr[], int n)
    {
     
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        {
            swap(arr, i, (n + ~i + 1) + ~1 + 1);
        }
    }
  
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 5, 3, 7, 2, 1, 6 };
        int n = arr.length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python program to reverse an array without 
# using "-" sign 
  
# Function to reverse array 
def reverseArray(arr, n):
  
    # Reverse array in simple manner 
    for i in range(n//2): 
  
        # Swap ith index value with (n-i-1)th 
        # index value 
        # Note : A - B = A + ~B + 1 
        # So n - i = n + ~i + 1 then 
        # n - i - 1 = (n + ~i + 1) + ~1 + 1 
        arr[i], arr[(n + ~i + 1) + ~1 + 1] = arr[(n + ~i + 1) + ~1 + 1],arr[i]
  
# Driver code 
  
arr = [ 5, 3, 7, 2, 1, 6 ] 
n = len(arr)
  
reverseArray(arr, n)
  
# print the reversed array 
for i in range(n):
    print(arr[i],end=" ")
  
# This code is contributed by ankush_953


C#
// C# program to reverse an array without
// using "-" sign
using System;
  
class GFG {
  
    // Function to reverse array
    static void reverseArray(int[] arr, int n)
    {
  
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        {
            swap(arr, i, (n + ~i + 1) + ~1 + 1);
        }
    }
  
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 5, 3, 7, 2, 1, 6 };
        int n = arr.Length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}
  
// This code has been contributed by 29AjayKumar


PHP


输出:

6 1 2 7 3 5

方法4:
在此方法4中,想法是使用按位运算运算符来实现减法,即
A – B = A +〜B + 1
因此,i–可以写成i = i +〜1 +1

C++

// C++ program to reverse an array without
// using "-" sign
#include 
using namespace std;
  
// Function to reverse array
void reverseArray(int arr[], int n)
{
  
    // Reverse array in simple manner
    for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        swap(arr[i], arr[(n + ~i + 1) + ~1 + 1]);
}
  
// Driver code
int main()
{
    int arr[] = { 5, 3, 7, 2, 1, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    reverseArray(arr, n);
  
    // print the reversed array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}

Java

// Java program to reverse an array without
// using "-" sign
import java.util.Arrays;
  
class GFG {
  
    // Function to reverse array
    static void reverseArray(int arr[], int n)
    {
     
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        {
            swap(arr, i, (n + ~i + 1) + ~1 + 1);
        }
    }
  
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 5, 3, 7, 2, 1, 6 };
        int n = arr.length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
  
// This code contributed by Rajput-Ji

Python3

# Python program to reverse an array without 
# using "-" sign 
  
# Function to reverse array 
def reverseArray(arr, n):
  
    # Reverse array in simple manner 
    for i in range(n//2): 
  
        # Swap ith index value with (n-i-1)th 
        # index value 
        # Note : A - B = A + ~B + 1 
        # So n - i = n + ~i + 1 then 
        # n - i - 1 = (n + ~i + 1) + ~1 + 1 
        arr[i], arr[(n + ~i + 1) + ~1 + 1] = arr[(n + ~i + 1) + ~1 + 1],arr[i]
  
# Driver code 
  
arr = [ 5, 3, 7, 2, 1, 6 ] 
n = len(arr)
  
reverseArray(arr, n)
  
# print the reversed array 
for i in range(n):
    print(arr[i],end=" ")
  
# This code is contributed by ankush_953

C#

// C# program to reverse an array without
// using "-" sign
using System;
  
class GFG {
  
    // Function to reverse array
    static void reverseArray(int[] arr, int n)
    {
  
        // Reverse array in simple manner
        for (int i = 0; i < n / 2; i++)
  
        // Swap ith index value with (n-i-1)th
        // index value
        // Note : A - B = A + ~B + 1
        // So n - i = n + ~i + 1 then
        // n - i - 1 = (n + ~i + 1) + ~1 + 1
        {
            swap(arr, i, (n + ~i + 1) + ~1 + 1);
        }
    }
  
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 5, 3, 7, 2, 1, 6 };
        int n = arr.Length;
  
        reverseArray(arr, n);
  
        // print the reversed array
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}
  
// This code has been contributed by 29AjayKumar

的PHP

输出:

6 1 2 7 3 5