算法leetcode|1656,GitHub重磅官宣

36 阅读2分钟
  • 1 <= id <= n
  • value.length == 5
  • value 仅由小写字母组成
  • 每次调用 insert 都会使用一个唯一的 id
  • 恰好调用 n 次 insert

原题传送门:

leetcode.cn/problems/de…


分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 好像照着题意做就行了,唯一要考虑的是value用什么结构去存储,数组刚刚好。

题解

rust

struct OrderedStream {
    values: Vec<String>,
    ptr: usize,
}


/\*\*
 \* `&self` means the method takes an immutable reference.
 \* If you need a mutable reference, change it to `&mut self` instead.
 \*/
impl OrderedStream {

    fn new(n: i32) -> Self {
        OrderedStream {
            values: vec!["".to\_string(); n as usize],
            ptr: 0,
        }
    }

    fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
        self.values[(id_key - 1) as usize] = value;
        let mut res = Vec::new();
        while self.ptr < self.values.len() && self.values[self.ptr] != "" {
            res.push(self.values[self.ptr].clone());
            self.ptr += 1;
        }
        res
    }
}

/\*\*
 \* Your OrderedStream object will be instantiated and called as such:
 \* let obj = OrderedStream::new(n);
 \* let ret\_1: Vec<String> = obj.insert(idKey, value);
 \*/


go

type OrderedStream struct {
	values []string
	ptr    int
}

func Constructor(n int) OrderedStream {
	return OrderedStream{
		values: make([]string, n),
		ptr:    0,
	}
}

func (this \*OrderedStream) Insert(idKey int, value string) []string {
	this.values[idKey-1] = value
	start := this.ptr
	for this.ptr < len(this.values) && this.values[this.ptr] != "" {
		this.ptr++
	}
	return this.values[start:this.ptr]
}


/\*\*
 \* Your OrderedStream object will be instantiated and called as such:
 \* obj := Constructor(n);
 \* param\_1 := obj.Insert(idKey,value);
 \*/


c++

class OrderedStream {
private:
    vector<string> values;
    int ptr;
public:
    OrderedStream(int n) {
        values.resize(n);
        ptr = 0;
    }

    vector<string> insert(int idKey, string value) {
        values[idKey - 1] = value;
        vector<string> res;
        while (ptr < values.size() && values[ptr] != "") {
            res.emplace\_back(values[ptr]);
            ++ptr;
        }
        return res;
    }
};

/\*\*
 \* Your OrderedStream object will be instantiated and called as such:
 \* OrderedStream\* obj = new OrderedStream(n);
 \* vector<string> param\_1 = obj->insert(idKey,value);
 \*/


java

class OrderedStream {
    private final String[] values;
    private int ptr;

    public OrderedStream(int n) {
        values = new String[n];
        ptr = 0;
    }

    public List<String> insert(int idKey, String value) {
        values[idKey - 1] = value;
        List<String> res = new ArrayList<>();
        while (ptr < values.length && values[ptr] != null) {
            res.add(values[ptr]);
            ++ptr;
        }
        return res;
    }
}

/\*\*
 \* Your OrderedStream object will be instantiated and called as such:
 \* OrderedStream obj = new OrderedStream(n);
 \* List<String> param\_1 = obj.insert(idKey,value);
 \*/


python

class OrderedStream:

    def \_\_init\_\_(self, n: int):
        self.values = [""] \* n
        self.ptr = 0

    def insert(self, idKey: int, value: str) -> List[str]:
        self.values[idKey - 1] = value
        res = []
        while self.ptr < len(self.values) and self.values[self.ptr]:
            res.append(self.values[self.ptr])
            self.ptr += 1
        return res


# Your OrderedStream object will be instantiated and called as such:
# obj = OrderedStream(n)
# param\_1 = obj.insert(idKey,value)



非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!