r0u*_*u1i 136
考虑由哈希表H和数组A组成的数据结构.哈希表键是数据结构中的元素,值是它们在数组中的位置.
因为数组需要自动增加大小,所以要分摊O(1)来添加一个元素,但我想这没关系.
对于这个问题我将使用两个数据结构
脚步 :-
代码 :-
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class JavaApplication1 {
    public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
        ArrayList<Integer> al =new ArrayList<Integer>();
        HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();  
        while(true){
            System.out.println("**menu**");
            System.out.println("1.insert");
            System.out.println("2.remove");
            System.out.println("3.search");
            System.out.println("4.rendom");
            int ch = sc.nextInt();
            switch(ch){
                case 1 : System.out.println("Enter the Element ");
                        int a = sc.nextInt();
                        if(mp.containsKey(a)){
                            System.out.println("Element is already present ");
                        }
                        else{
                            al.add(a);
                            mp.put(a, al.size()-1);
                        }
                        break;
                case 2 : System.out.println("Enter the Element Which u want to remove");
                        a = sc.nextInt();
                        if(mp.containsKey(a)){
                            int size = al.size();
                            int index = mp.get(a);
                            int last = al.get(size-1);
                            Collections.swap(al, index,  size-1);
                            al.remove(size-1);
                            mp.put(last, index);
                            System.out.println("Data Deleted");
                        }
                        else{
                            System.out.println("Data Not found");
                        }
                        break;
                case 3 : System.out.println("Enter the Element to Search");
                        a = sc.nextInt();
                        if(mp.containsKey(a)){
                            System.out.println(mp.get(a));
                        }
                        else{
                            System.out.println("Data Not Found");
                        }
                        break;
                case 4 : Random rm = new Random();
                        int index = rm.nextInt(al.size());
                        System.out.println(al.get(index));
                        break;
            }
        }
    }
}
-- 时间复杂度 O(1)。-- 空间复杂度O(N)。
您可能不喜欢这样,因为他们可能正在寻找一个聪明的解决方案,但有时还是值得坚持…… 哈希表已经满足要求 -总体上可能比其他任何方法都更好(尽管显然摊销后的常量时间,以及对其他解决方案的不同妥协)。
棘手的要求是选择“随机元素”:在哈希表中,您需要扫描或探查此类元素。
对于封闭式散列/开放式寻址,任何给定存储桶被占用的机会为size() / capacity(),但至关重要的是,哈希表实现通常将其保持在恒定的乘法范围内(例如,该表可能比其当前内容大1.2倍)。到约10倍,具体取决于性能/内存调整)。这意味着我们平均可以预期搜索1.2到10个桶-完全独立于容器的总大小;摊销O(1)。
我可以想象出两种简单的方法(还有很多更巧妙的方法):
从随机桶中线性搜索
反复尝试随机存储桶,直到找到一个填充的桶
这不是一个很好的解决方案,但是与始终保持第二个索引数组的内存和性能开销相比,这可能是一个更好的总体折衷方案。