哈希表常见的三个操作时put、get和containsKey,而且这三个操作的时间复杂度为O(1)。现在想加一个setAll功能,就是把所有记录value都设成统一的值。请设计并实现这种有setAll功能的哈希表,并且put、get、containsKey和setAll四个操作的时间复杂度都为O(1)。
[友情提示]: C++选手若有需要可以使用unordered_map替换map来将复杂度从O(log n)降为O(1)
第一行一个整数N表示操作数。
接下来N行,每行第一个数字opt代表操作类型
若opt=1,接下来有两个整数x, y表示设置key=x对应的value=y
若opt=2,接下来一个整数x,表示查询key=x对应的value,若key=x不存在输出-1
若opt=3,接下来一个整数x,表示把加入过的所有的key对应的value都设置为x
对于每个操作2,输出一个整数表示答案
6 1 1 2 2 1 2 2 3 4 2 1 2 2
2 -1 4 -1
#include <bits/stdc++.h> using namespace std; class { unordered_map<int, int> M, U; int all; public: void put(int x, int y){ auto it = M.find(x); if(it != M.end()) M.erase(it); U[x] = y; } int get(int x){ if(M.find(x) != M.end()) return all; if(U.find(x)==U.end()) return -1; else return U[x]; } void setAll(int x){ for(auto it:U) M[it.first] = 0; U.clear(); all = x; } }P; int main(){ int n, opt, x, y; cin>>n; for(int i=0;i<n;i++){ cin>>opt>>x; if(opt==1){ cin>>y; P.put(x, y); }else if(opt==2){ cout<<P.get(x)<<endl; }else if(opt==3){ P.setAll(x); } } return 0; }
import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { HashMap<Integer, Integer> map = new HashMap<>(); HashSet<Integer> memory = new HashSet<>(); int allVal = 0; int n = sc.nextInt(); for(int i = 0; i < n; i++){ int op = sc.nextInt(); if(op == 1){ int key = sc.nextInt(); int val = sc.nextInt(); map.put(key, val); memory.add(key); }else if(op == 2){ int key = sc.nextInt(); if(map.containsKey(key)) System.out.println(map.get(key)); else if(memory.contains(key)) System.out.println(allVal); else System.out.println(-1); }else if(op == 3){ map.clear(); allVal = sc.nextInt(); } } } } }
class MyHash: def __init__(self) -> None: self.map = {} self.memory = set() self.all_val = 0 def put(self, key, val): self.map[key] = val self.memory.add(key) def get(self, key): if key in self.map: return self.map[key] elif key in self.memory: return self.all_val else: return -1 def set_all(self, val): self.map.clear() self.all_val = val def main(): myhash = MyHash() N = int(input()) for _ in range(N): tmp_list = list(map(int, input().split())) if tmp_list[0] == 1: myhash.put(tmp_list[1], tmp_list[2]) elif tmp_list[0] == 2: print(myhash.get(tmp_list[1])) else: myhash.set_all(tmp_list[1]) main()
#模拟题 #按照题目所说操作进行模拟 n=int(input()) hash_list=[] for i in range(n): input_data=list(map(int,input().split())) if input_data[0]==1: hash_list.append([input_data[1],input_data[2]]) if input_data[0]==2: flag=False for item in hash_list: if item[0]==input_data[1]: print(item[1]) flag=True break if not flag: print(-1) if input_data[0]==3: for item in hash_list: item[1]=input_data[1]
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ Map<Integer,Integer> map=new HashMap<>(); Set<Integer> set=new HashSet<>(); StringBuilder sb=new StringBuilder(); int allVal=0; int n=sc.nextInt(); for(int i=0;i<n;i++){ int op=sc.nextInt(); if(op==1){ int key=sc.nextInt(); int val=sc.nextInt(); map.put(key,val); set.add(key); }else if(op==2){ int key=sc.nextInt(); if(map.containsKey(key)) sb.append(map.get(key)); else if(set.contains(key)) sb.append(allVal); else sb.append(-1); sb.append("\n"); }else if(op==3){ map.clear(); allVal=sc.nextInt(); } } System.out.println(sb.toString()); } sc.close(); } }
import java.util.*; import java.io.*; import java.io.*; import java.util.*; class newHash{ private Map<String,myValue> hm; private long time; private myValue allv; public newHash(){ hm = new HashMap(); time = 0; } public void put(String key,String value){ hm.put(key,new myValue(value,this.time++)); } public String get(String key){ myValue res = hm.get(key); if(res==null){ return "-1"; }else{ if(allv!=null && allv.time>res.time){ return allv.value; }else{ return res.value; } } } public void setAll(String value){ allv = new myValue(value,this.time++); } } class myValue{ String value; long time; public myValue(String v,long t){ this.value=v; this.time=t; } } public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); newHash nh = new newHash(); StringBuilder sb = new StringBuilder(); for(int i=0;i<N;i++){ String[] s = br.readLine().split(" "); switch(s[0]){ case "1": nh.put(s[1],s[2]);break; case "2": sb.append(nh.get(s[1])+"\n");break; case "3": nh.setAll(s[1]);break; } } System.out.print(sb.toString()); } }