本文概述
- C ++
- Java
- Python 3
- C#
- 的PHP
例子:
Input: arr[] = { 2, 3, 3, 5, 5 }
Output: 0 1 1 7 7
Bitwise Xor of 3, 3, 5, 5 = 0
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 3, 5 = 7
Bitwise Xor of 2, 3, 3, 5 = 7Input : arr[] = { 1, 2, 3 }
Output : 1 2 3
方法:
- 首先, 对数组所有元素进行按位异或, 并将其存储在变量let X中。
- 现在, 将每个元素替换为X和该元素的异或, 因为对同一个元素进行异或将抵消所有其他元素的异或。
- 打印修改后的数组。
C ++
//C++ program to Replace every element
//by the bitwise xor of all other elements
#include <
bits/stdc++.h>
using namespace std;
//Function to replace the elements
void ReplaceElements( int arr[], int n)
{
int X = 0;
//Calculate the xor of all the elements
for ( int i = 0;
i <
n;
++i) {
X ^= arr[i];
}//Replace every element by the
//xor of all other elements
for ( int i = 0;
i <
n;
++i) {
arr[i] = X ^ arr[i];
}
}//Driver code
int main()
{
int arr[] = { 2, 3, 3, 5, 5 };
int n = sizeof (arr) /sizeof (arr[0]);
ReplaceElements(arr, n);
//Print the modified array.
for ( int i = 0;
i <
n;
++i) {
cout <
<
arr[i] <
<
" " ;
}
return 0;
}
Java
//Javaprogram to Replace every element
//by the bitwise xor of all other elements import java.io.*;
class GFG {//Function to replace the elements
static void ReplaceElements( int arr[], int n)
{
int X = 0 ;
//Calculate the xor of all the elements
for ( int i = 0 ;
i <
n;
++i) {
X ^= arr[i];
} //Replace every element by the
//xor of all other elements
for ( int i = 0 ;
i <
n;
++i) {
arr[i] = X ^ arr[i];
}
} //Driver code
public static void main (String[] args) {int arr[] = { 2 , 3 , 3 , 5 , 5 };
int n = arr.length;
ReplaceElements(arr, n);
//Print the modified array.
for ( int i = 0 ;
i <
n;
++i) {
System.out.print(arr[i] + " " );
}
}
}
Python 3
# Python 3 program to Replace every element
# by the bitwise xor of all other elements# Function to replace the elements
def ReplaceElements(arr, n):X = 0# Calculate the xor of all the elements
for i in range (n):
X ^ = arr[i]# Replace every element by the
# xor of all other elements
for i in range (n):
arr[i] = X ^ arr[i]# Driver code
if __name__ = = "__main__" :
arr = [ 2 , 3 , 3 , 5 , 5 ]
n = len (arr)ReplaceElements(arr, n)# Print the modified array.
for i in range (n):
print (arr[i], end = " " )# This code is contributed
# by ChitraNayal
C#
//C#program to Replace every element
//by the bitwise xor of all other elements
using System;
public class GFG{
//Function to replace the elements
static void ReplaceElements( int []arr, int n)
{
int X = 0;
//Calculate the xor of all the elements
for ( int i = 0;
i <
n;
++i) {
X ^= arr[i];
} //Replace every element by the
//xor of all other elements
for ( int i = 0;
i <
n;
++i) {
arr[i] = X ^ arr[i];
}
} //Driver code static public void Main (){int []arr = { 2, 3, 3, 5, 5 };
int n = arr.Length;
ReplaceElements(arr, n);
//Print the modified array.
for ( int i = 0;
i <
n;
++i) {
Console.Write(arr[i] + " " );
}
}
//This code is contributed by ajit
}
的PHP
<
?php
//PHP program to Replace every element
//by the bitwise xor of all other elements //Function to replace the elements
function ReplaceElements( $arr , $n )
{
$X = 0;
//Calculate the xor of all the elements
for ( $i = 0;
$i <
$n ;
++ $i )
{
$X ^= $arr [ $i ];
} //Replace every element by the
//xor of all other elements
for ( $i = 0;
$i <
$n ;
++ $i )
{
$arr [ $i ] = $X ^ $arr [ $i ];
}
return $arr ;
} //Driver code
$arr = array ( 2, 3, 3, 5, 5 );
$n = sizeof( $arr );
$arr1 = ReplaceElements( $arr , $n );
//Print the modified array.
for ( $i = 0;
$i <
$n ;
++ $i )
{
echo ( $arr1 [ $i ] . " " );
}//This code is contributed
//by Mukul singh
?>
输出如下:
0 1 1 7 7
【用所有其他的BitWise XOR替换数组的每个元素】时间复杂度: O(n)
推荐阅读
- 算法题(递归程序打印三角形图案)
- 算法设计(在给定大小的组中反向链表|S2)
- 生成长度为n的所有二进制字符串,其中子字符串“01”恰好出现两次
- 在R编程中从向量创建数据框
- GTX和RTX–哪个更好(GTX 1080Ti和RTX 2080有什么区别?)
- 算法题(求最长连续子序列)
- 算法题(检测并删除链表中的循环)
- 算法题(快速选择算法)
- Win8双系统更改选择系统的等待时间的办法