日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode1146. 快照数组 | Snapshot Array

發布時間:2023/12/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode1146. 快照数组 | Snapshot Array 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(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","set","get"] [[3],[0,5],[],[0,6],[0,0]] Output: [null,null,0,null,5] Explanation: SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3 snapshotArr.set(0,5); // Set array[0] = 5 snapshotArr.snap(); // Take a snapshot, return snap_id = 0 snapshotArr.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","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

480ms 1 class SnapshotArray { 2 3 private var dict = [Int: Int]() 4 private var snapshots = [[Int: Int]]() 5 6 init(_ length: Int) { 7 //array = Array(repeating: 0, count: length + 1) 8 } 9 10 func set(_ index: Int, _ val: Int) { 11 dict[index] = val 12 } 13 14 func snap() -> Int { 15 snapshots.append(dict) 16 return snapshots.count - 1 17 } 18 19 func get(_ index: Int, _ snap_id: Int) -> Int { 20 return snapshots[snap_id][index] ?? 0 21 } 22 } 23 24 /** 25 * Your SnapshotArray object will be instantiated and called as such: 26 * let obj = SnapshotArray(length) 27 * obj.set(index, val) 28 * let ret_2: Int = obj.snap() 29 * let ret_3: Int = obj.get(index, snap_id) 30 */

Runtime:?828 ms

Memory Usage:?27.8 MB 1 class SnapshotArray { 2 var vv:[[Int:Int]] = [[Int:Int]]() 3 4 init(_ length: Int) { 5 vv.append([Int:Int]()) 6 } 7 8 func set(_ index: Int, _ val: Int) { 9 vv[vv.count - 1][index] = val 10 } 11 12 func snap() -> Int { 13 vv.append([Int:Int]()) 14 return vv.count - 2 15 } 16 17 func get(_ index: Int, _ snap_id: Int) -> Int { 18 for i in stride(from:snap_id,through:0,by:-1) 19 { 20 if vv[i][index] != nil 21 { 22 return vv[i][index]! 23 } 24 } 25 return 0 26 } 27 } 28 29 /** 30 * Your SnapshotArray object will be instantiated and called as such: 31 * let obj = SnapshotArray(length) 32 * obj.set(index, val) 33 * let ret_2: Int = obj.snap() 34 * let ret_3: Int = obj.get(index, snap_id) 35 */

?

轉載于:https://www.cnblogs.com/strengthen/p/11297779.html

總結

以上是生活随笔為你收集整理的[Swift]LeetCode1146. 快照数组 | Snapshot Array的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。