📌  相关文章
📜  使用给定掩码对给定数组应用卷积后查找结果数组

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

使用给定掩码对给定数组应用卷积后查找结果数组

给定两个包含N个整数的数组arr[]和一个奇数大小的mask[] 。任务是用通过对数组执行相同卷积计算的值替换每个数组元素。

例子:

方法:解决这个问题最简单的方法是使用嵌套循环。外循环将从左到右遍历数组,即从i = 0 到 i < N ,内循环将从索引i - K/2到索引i + K/2遍历掩码并计算卷积他们。最后,打印输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to perform same convolution
void ComputeSameConvolution(int arr[],
                            int mask[], int N,
                            int K)
{
    int i, j, k, sum;
 
    // Nested loops
    for (i = 0; i < N; i++) {
        sum = 0;
        k = 0;
        for (j = i - (K / 2); j <= i + (K / 2);
             j++) {
            if (j < 0 || j >= N)
                k++;
            else
                sum += arr[j] * mask[k++];
        }
 
        // Print the required sum
        cout << sum << ' ';
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
    int mask[] = { 1, 2, -1 };
 
    int K = sizeof(mask) / sizeof(mask[0]);
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    ComputeSameConvolution(arr, mask, N, K);
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
  // Function to perform same convolution
  static void ComputeSameConvolution(int[] arr,
                                     int[] mask, int N,
                                     int K)
  {
    int i, j, k, sum;
 
    // Nested loops
    for (i = 0; i < N; i++) {
      sum = 0;
      k = 0;
      for (j = i - (K / 2); j <= i + (K / 2); j++) {
        if (j < 0 || j >= N)
          k++;
        else
          sum += arr[j] * mask[k++];
      }
 
      // Print the required sum
      System.out.print(sum + " ");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 9, 7, 3, 9, 1, 8, 11 };
    int[] mask = { 1, 2, -1 };
 
    int K = mask.length;
    int N = arr.length;
 
    // Function Call
    ComputeSameConvolution(arr, mask, N, K);
  }
}
 
// This code is contributed by ukasp.


Python3
# Python code for the above approach
 
# Function to perform same convolution
def ComputeSameConvolution(arr, mask, N, K):
    i = None
    j = None
    k = None
    sum = None
 
    # Nested loops
    for i in range(N):
        sum = 0;
        k = 0;
        for j in range(i - (K // 2), i + (K // 2) + 1):
            if (j < 0 or j >= N):
                k += 1
            else:
                sum += arr[j] * mask[k];
                k += 1
 
        # Print the required sum
        print(sum, end=' ');
 
# Driver Code
arr = [9, 7, 3, 9, 1, 8, 11];
mask = [1, 2, -1];
 
K = len(mask)
N = len(arr)
 
# Function Call
ComputeSameConvolution(arr, mask, N, K);
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to perform same convolution
static void ComputeSameConvolution(int []arr,
                            int []mask, int N,
                            int K)
{
    int i, j, k, sum;
 
    // Nested loops
    for (i = 0; i < N; i++) {
        sum = 0;
        k = 0;
        for (j = i - (K / 2); j <= i + (K / 2);
             j++) {
            if (j < 0 || j >= N)
                k++;
            else
                sum += arr[j] * mask[k++];
        }
 
        // Print the required sum
        Console.Write(sum + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int []arr = { 9, 7, 3, 9, 1, 8, 11 };
    int []mask = { 1, 2, -1 };
     
    int K = mask.Length;
    int N = arr.Length;
     
    // Function Call
    ComputeSameConvolution(arr, mask, N, K);
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
11 20 4 20 3 6 30 

时间复杂度: O(N*K)
辅助空间: O(1)