三、快照数组(Weekly Contest 148)

题目描述:
实现支持下列接口的「快照数组」- SnapshotArray:

SnapshotArray(int length) - 初始化一个与指定长度相等的 类数组 的数据结构。初始时,每个元素都等于 0。
void set(index, val) - 会将指定索引 index 处的元素设置为 val。
int snap() - 获取该数组的快照,并返回快照的编号 snap_id(快照号是调用 snap() 的总次数减去 1)。
int get(index, snap_id) - 根据指定的 snap_id 选择快照,并返回该快照指定索引 index 的值。

示例:

输入:[“SnapshotArray”,“set”,“snap”,“set”,“get”]
[[3],[0,5],[],[0,6],[0,0]]
输出:[null,null,0,null,5]
解释:
SnapshotArray snapshotArr = new SnapshotArray(3); // 初始化一个长度为 3 的快照数组
snapshotArr.set(0,5); // 令 array[0] = 5
snapshotArr.snap(); // 获取快照,返回 snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // 获取 snap_id = 0 的快照中 array[0] 的值,返回 5

提示:

1 <= length <= 50000
题目最多进行50000 次set,snap,和 get的调用 。
0 <= index < length
0 <= snap_id < 我们调用 snap() 的总次数
0 <= val <= 10^9

没整明白这道题的意思
就是说我们每次调用一次sanp就会产生一个新的数组,这个数组会复制前面的一个数组,但是后面的赋值会覆盖前面的赋值,我们考虑将length作为key的键,思路有点反人类,每次我们将对应的index的版本作为(index)作为键,而值又是一个map,以此类推


class SnapshotArray {
    Map<Integer,Map<Integer,Integer>> map = null;
    int sna = 0;
    public SnapshotArray(int length) {
        map = new HashMap<>(length);
        for (int i = 0; i < length; i++) {
            map.put(i,new HashMap<>());
        }
    }

    public void set(int index, int val) {
        Map<Integer, Integer> map = this.map.get(index);
        map.put(sna,val);
        this.map.put(index,map);
    }

    public int snap() {
        return sna ++;

    }

    public int get(int index, int snap_id) {
        while (snap_id >= 0){
            if(map.get(index).get(snap_id) != null){
                return map.get(index).get(snap_id);
            }
            snap_id --;
        }
        return 0;
    }
}

学习一下别人的代码:

class SnapshotArray {
		List<List<Integer>> times;
		List<List<Integer>> values;
		int time;
	    public SnapshotArray(int n) {
	    	time = 0;
	        times = new ArrayList<List<Integer>>();
	        values = new ArrayList<List<Integer>>();
	        for(int i = 0;i < n;i++){
	        	times.add(new ArrayList<Integer>());
	        	times.get(i).add(0);
	        	values.add(new ArrayList<Integer>());
	        	values.get(i).add(0);
	        }
	    }
	    
	    public void set(int index, int val) {
	        if(times.get(index).get(times.get(index).size()-1).equals(time)){
	        	values.get(index).set(values.get(index).size()-1, val);
	        }else{
	        	times.get(index).add(time);
	        	values.get(index).add(val);
	        }
	    }
	    
	    public int snap() {
	        return time++;
	    }
	    
	    public int get(int index, int snap_id) {
	        int ind = Collections.binarySearch(times.get(index), snap_id);
	        if(ind < 0)ind = -ind-2;
	        return values.get(index).get(ind);
	    }
	}	

这个代码可能会比较清晰一点,因为数组的长度为length,所以map的key是对应的下标,这样有点逆向而行

class SnapshotArray {

		  private Map<Integer, Map<Integer, Integer>> snaps = null;
		    private int snapId = 0;

		    public SnapshotArray(int length) {
		        snaps = new HashMap<>(length);
		        while (length-- > 0) {
		            snaps.put(length, new HashMap<Integer, Integer>());
		        }
		    }

		    public void set(int index, int val) {
		        snaps.get(index).put(snapId, val);
		    }

		    public int snap() {
		        return snapId++;
		    }

		    public int get(int index, int snap_id) {
		        while(snap_id>-1){//关键在这,因为在snap()生成新快照,没有为每个新生成的快照添加数据
		            if (snaps.get(index).get(snap_id) != null) {
		                return snaps.get(index).get(snap_id);
		            }
		            --snap_id;
		        }
		        return 0;
		    }

	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值