![[Swift]LeetCode1146. 快照数组 | Snapshot Array,第1张 [Swift]LeetCode1146. 快照数组 | Snapshot Array,第1张](/aiimages/%5BSwift%5DLeetCode1146.+%E5%BF%AB%E7%85%A7%E6%95%B0%E7%BB%84+%7C+Snapshot+Array.png)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公众号:山青咏芝(shanqingyongzhi)
?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11297779.html
?如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
?原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length) initializes an array-like data structure with the given length. Initially,each element equals 0. voID set(index,val) sets the element at the given index to be equal to val. int snap() takes a snapshot of the array and returns the snap_ID: the total number of times we called snap() minus 1. int get(index,snap_ID) returns the value at the given index,at the time we took the snapshot with the given snap_ID Example 1:
input: ["SnapshotArray","set","snap","get"][[3],[0,5],[],6],0]]Output: [null,null,5]Explanation: SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3snapshotArr.set(0,5); // Set array[0] = 5snapshotArr.snap(); // Take a snapshot,return snap_ID = 0snapshotArr.set(0,6);snapshotArr.get(0,0); // Get the value of array[0] with snap_ID = 0,return 5
Constraints:
1 <= length <= 50000 At most 50000 calls will be made to set, snap,and get. 0 <= index < length 0 <= snap_ID < (the total number of times we call snap()) 0 <= val <= 10^9 实现支持下列接口的「快照数组」- 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","get"] [[3],0]]输出:[null,5]解释:SnapshotArray snapshotArr = new SnapshotArray(3); // 初始化一个长度为 3 的快照数组snapshotArr.set(0,5); // 令 array[0] = 5snapshotArr.snap(); // 获取快照,返回 snap_ID = 0snapshotArr.set(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 总结 以上是内存溢出为你收集整理的[Swift]LeetCode1146. 快照数组 | Snapshot Array全部内容,希望文章能够帮你解决[Swift]LeetCode1146. 快照数组 | Snapshot Array所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)