📜  允许负数的索引映射(或普通散列)

📅  最后修改于: 2021-10-27 07:57:59             🧑  作者: Mango

给定一个范围有限的数组,它包含正数和非正数,即元素在 -MAX 到 +MAX 的范围内。我们的任务是在 O(1) 时间内搜索数组中是否存在某个数字。
由于范围有限,我们可以使用索引映射(或普通散列)。我们使用值作为大数组中的索引。因此我们可以在 O(1) 时间内搜索和插入元素。

地图

如何处理负数?
这个想法是使用大小为 hash[MAX+1][2] 的二维数组
算法:

Assign all the values of the hash matrix as 0.
Traverse the given array:
    If the element ele is non negative assign 
        hash[ele][0] as 1.
    Else take the absolute value of ele and 
         assign hash[ele][1] as 1.

搜索数组中的任何元素x

  • 如果 X 是非负检查 hash[X][0] 是否为 1。如果 hash[X][0] 为 1,则该数字存在,否则不存在。
  • 如果 X 为负,取 X 的绝对值,然后检查 hash[X][1] 是否为 1。如果 hash[X][1] 为 1,则该数字存在

下面是上述想法的实现。

C++
// CPP program to implement direct index mapping
// with negative values allowed.
#include 
using namespace std;
#define MAX 1000
  
// Since array is global, it is initialized as 0.
bool has[MAX + 1][2];
  
// searching if X is Present in the given array 
// or not.
bool search(int X)
{
    if (X >= 0) {
        if (has[X][0] == 1)
            return true;
        else
            return false;
    }
  
    // if X is negative take the absolute 
    // value of X.
    X = abs(X);
    if (has[X][1] == 1)
        return true;
  
    return false;
}
  
void insert(int a[], int n)
{
    for (int i = 0; i < n; i++) {
        if (a[i] >= 0) 
            has[a[i]][0] = 1;
       else
            has[abs(a[i])][1] = 1;
    }
}
  
// Driver code
int main()
{
    int a[] = { -1, 9, -5, -8, -5, -2 };
    int n = sizeof(a)/sizeof(a[0]);
    insert(a, n);
    int X = -5;
    if (search(X) == true)
       cout << "Present"; 
    else
       cout << "Not Present";
    return 0;
}


Java
// Java program to implement direct index 
// mapping with negative values allowed. 
class GFG
{
  
final static int MAX = 1000;
  
// Since array is global, it 
// is initialized as 0. 
static boolean[][] has = new boolean[MAX + 1][2];
  
// searching if X is Present in 
// the given array or not. 
static boolean search(int X) 
{
    if (X >= 0) 
    {
        if (has[X][0] == true) 
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }
  
    // if X is negative take the 
    // absolute value of X. 
    X = Math.abs(X);
    if (has[X][1] == true) 
    {
        return true;
    }
  
    return false;
}
  
static void insert(int a[], int n) 
{
    for (int i = 0; i < n; i++) 
    {
        if (a[i] >= 0) 
        {
            has[a[i]][0] = true;
        } 
        else 
        {
            int abs_i = Math.Abs(a[i]);
            has[abs_i][1] = true;
        }
    }
}
  
// Driver code 
public static void main(String args[]) 
{
    int a[] = {-1, 9, -5, -8, -5, -2};
    int n = a.length;
    insert(a, n);
    int X = -5;
    if (search(X) == true)
    {
        System.out.println("Present");
    } 
    else 
    {
        System.out.println("Not Present");
    }
}
}
  
// This code is contributed
// by 29AjayKumar


Python3
# Python3 program to implement direct index 
# mapping with negative values allowed.
  
# Searching if X is Present in the 
# given array or not.
def search(X):
  
    if X >= 0:
        return has[X][0] == 1
  
    # if X is negative take the absolute
    # value of X.
    X = abs(X)
    return has[X][1] == 1
  
def insert(a, n):
  
    for i in range(0, n):
        if a[i] >= 0:
            has[a[i]][0] = 1
        else:
            has[abs(a[i])][1] = 1
  
# Driver code
if __name__ == "__main__":
  
    a = [-1, 9, -5, -8, -5, -2]
    n = len(a)
  
    MAX = 1000
      
    # Since array is global, it is
    # initialized as 0.
    has = [[0 for i in range(2)] 
              for j in range(MAX + 1)]
    insert(a, n)
  
    X = -5
    if search(X) == True:
        print("Present")
    else:
        print("Not Present")
  
# This code is contributed by Rituraj Jain


C#
// C# program to implement direct index 
// mapping with negative values allowed. 
using System;
  
class GFG
{
  
static int MAX = 1000;
  
// Since array is global, it 
// is initialized as 0. 
static bool[,] has = new bool[MAX + 1, 2];
  
// searching if X is Present in 
// the given array or not. 
static bool search(int X) 
{
    if (X >= 0) 
    {
        if (has[X, 0] == true) 
        {
            return true;
        } 
        else
        {
            return false;
        }
    }
  
    // if X is negative take the 
    // absolute value of X. 
    X = Math.Abs(X);
    if (has[X, 1] == true) 
    {
        return true;
    }
  
    return false;
}
  
static void insert(int[] a, int n) 
{
    for (int i = 0; i < n; i++) 
    {
        if (a[i] >= 0) 
        {
            has[a[i], 0] = true;
        } 
        else
        {    
            int abs_i = Math.Abs(a[i]);
            has[abs_i, 1] = true;
        }
    }
}
  
// Driver code 
public static void Main() 
{
    int[] a = {-1, 9, -5, -8, -5, -2};
    int n = a.Length;
    insert(a, n);
    int X = -5;
    if (search(X) == true)
    {
        Console.WriteLine("Present");
    } 
    else
    {
        Console.WriteLine("Not Present");
    }
}
}
  
// This code is contributed
// by Akanksha Rai


Javascript


输出:

Present

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程