• 【Java|golang】1656. 设计有序流


    有 n 个 (id, value) 对,其中 id 是 1 到 n 之间的一个整数,value 是一个字符串。不存在 id 相同的两个 (id, value) 对。

    设计一个流,以 任意 顺序获取 n 个 (id, value) 对,并在多次调用时 按 id 递增的顺序 返回一些值。

    实现 OrderedStream 类:

    OrderedStream(int n) 构造一个能接收 n 个值的流,并将当前指针 ptr 设为 1 。
    String[] insert(int id, String value) 向流中存储新的 (id, value) 对。存储后:
    如果流存储有 id = ptr 的 (id, value) 对,则找出从 id = ptr 开始的 最长 id 连续递增序列 ,并 按顺序 返回与这些 id 关联的值的列表。然后,将 ptr 更新为最后那个 id + 1 。
    否则,返回一个空列表。

    示例:

    在这里插入图片描述

    输入
    [“OrderedStream”, “insert”, “insert”, “insert”, “insert”, “insert”]
    [[5], [3, “ccccc”], [1, “aaaaa”], [2, “bbbbb”], [5, “eeeee”], [4, “ddddd”]]
    输出
    [null, [], [“aaaaa”], [“bbbbb”, “ccccc”], [], [“ddddd”, “eeeee”]]

    解释
    OrderedStream os= new OrderedStream(5);
    os.insert(3, “ccccc”); // 插入 (3, “ccccc”),返回 []
    os.insert(1, “aaaaa”); // 插入 (1, “aaaaa”),返回 [“aaaaa”]
    os.insert(2, “bbbbb”); // 插入 (2, “bbbbb”),返回 [“bbbbb”, “ccccc”]
    os.insert(5, “eeeee”); // 插入 (5, “eeeee”),返回 []
    os.insert(4, “ddddd”); // 插入 (4, “ddddd”),返回 [“ddddd”, “eeeee”]

    class OrderedStream {
        String[] index;
        int ptr;
        public OrderedStream(int n) {
            index=new String[n+1];
            ptr=1;
        }
    
        public List<String> insert(int idKey, String value) {
            List<String> res=new ArrayList<>();
            index[idKey]=value;
            for (int i = ptr; i < index.length; i++,ptr++) {
                if(index[i]==null){
                    return res;
                }else {
                    res.add(index[i]);
                }
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    type OrderedStream struct {
        index []string
        ptr   int
    }
    
    
    func Constructor(n int) OrderedStream {
       return OrderedStream{
    	   index: make([]string,n+1),
    	   ptr:   1,
       }
    }
    
    
    func (this *OrderedStream) Insert(idKey int, value string) []string {
    	res := make([]string, 0)
        this.index[idKey]=value
    	for i:=this.ptr;i<len(this.index);i++{
    
    		if this.index[i]=="" {
    			 return res
    		}else {
    			res=append(res,this.index[i])
    			this.ptr++
    		}
    
    	}
       return res
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

  • 相关阅读:
    数据结构day3
    1.5.4 HDFS 客户端操作-hadoop-最全最完整的保姆级的java大数据学习资料
    Unity移动端游戏性能优化简谱之 以引擎模块为划分的CPU耗时调优
    Tomcat设计思路
    缓慢变化维度SCD
    算法小考试(有点难)
    Go Goroutine 究竟可以开多少?(详细介绍)
    最新国内maven仓库镜像地址
    Fiddler工具使用
    CSS 渐变锯齿消失术
  • 原文地址:https://blog.csdn.net/qq_44461217/article/details/126359947