• 力扣每日一题2022-08-16简单题:设计有序流



    题目描述

    1656.设计有序流


    思路

    模拟

    对于OrderedStream(int n),在初始化时创建一个长度为k+1的数组stream,用来存储后续的字符串。由于指针ptr的初始值为1,数组下标是从0开始的,所以使用长度n+1的数组可以使编码更方便。
    对于String[] insert(int id, String value),直接根据题意模拟即可。将stream[id]置为value,如果stream[id]不为空,就将其加入答案,并将ptr+1,知道指针超出边界或stream[id]为空时结束并返回答案。

    Python实现

    class OrderedStream:
    
        def __init__(self, n: int):
            self.stream = [""] * (n+1)
            self.ptr = 1
    
    
        def insert(self, idKey: int, value: str) -> List[str]:
            self.stream[idKey] = value
            ans = list()
            while self.ptr < len(self.stream) and self.stream[self.ptr]:
                ans.append(self.stream[self.ptr])
                self.ptr += 1
            return ans
    
    
    
    # Your OrderedStream object will be instantiated and called as such:
    # obj = OrderedStream(n)
    # param_1 = obj.insert(idKey,value)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Java实现

    class OrderedStream {
    
        private String[] stream;
        private int ptr;
    
        public OrderedStream(int n) {
            stream = new String[n+1];
            ptr = 1;
        }
        
        public List<String> insert(int idKey, String value) {
            stream[idKey] = value;
            List<String> ans = new ArrayList<String>();
            while (ptr < stream.length && stream[ptr] != null) {
                ans.add(stream[ptr]);
                ++ptr;
            }
            return ans;
        }
    }
    
    /**
     * Your OrderedStream object will be instantiated and called as such:
     * OrderedStream obj = new OrderedStream(n);
     * List param_1 = obj.insert(idKey,value);
     */
    
    • 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

    C++实现

    class OrderedStream {
    private:
        vector<string> stream;
        int ptr;
    
    public:
        OrderedStream(int n) {
            stream.resize(n+1);
            ptr = 1;
        }
        
        vector<string> insert(int idKey, string value) {
            stream[idKey] = value;
            vector<string> ans;
            while (ptr < stream.size() && !stream[ptr].empty()) {
                ans.push_back(stream[ptr]);
                ++ptr;
            }
            return ans;
        }
    };
    
    /**
     * Your OrderedStream object will be instantiated and called as such:
     * OrderedStream* obj = new OrderedStream(n);
     * vector param_1 = obj->insert(idKey,value);
     */
    
    • 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
  • 相关阅读:
    我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
    计算机毕业设计ssm+vue基本微信小程序的奶茶点单系统
    【STM32外设系列】JW01三合一空气质量检测模块
    Java基础面试题【1】
    Java / Android 线程间通信
    C++_基础语法
    Open vSwitch系列之数据结构解析深入分析ofpbuf
    TCP 的 Keepalive 和 HTTP 的 Keep-Alive 是一个东西吗?
    iOS 通过NSURLProtocol拦截WKWebView网络请求
    MySQL备份与恢复
  • 原文地址:https://blog.csdn.net/wcy1034036507/article/details/126358435