c*******u 发帖数: 47 | 1 求好心人帮我看看,想用in place, 就是过不了大的,折腾好久了,遇到这个的时候挂
了:
Last executed input
[[3,5],[12,15]], [6,6]
code:
class Solution {
public:
vector insert(vector &intervals, Interval newInterval) {
bool is_new_inserted = false;
for(vector::iterator it = intervals.begin(); it< intervals.end
(); it++){
if ((*it).end < newInterval.start){
continue;
}
if ((*it).start > newInterval.end){
if(!is_new_inserted){
intervals.insert(it, newInterval);
is_new_inserted = true;
}
continue;
}
newInterval.start = min(newInterval.start, (*it).start);
newInterval.end = max(newInterval.end, (*it).end);
intervals.erase(it);
it--;
}
if(!is_new_inserted)
intervals.push_back(newInterval);
return intervals;
}
}; | c*******u 发帖数: 47 | | B*******1 发帖数: 2454 | 3 弱问,vector,你insert,怎么in place的啊?
{
end
【在 c*******u 的大作中提到】 : 求好心人帮我看看,想用in place, 就是过不了大的,折腾好久了,遇到这个的时候挂 : 了: : Last executed input : [[3,5],[12,15]], [6,6] : code: : class Solution { : public: : vector insert(vector &intervals, Interval newInterval) { : bool is_new_inserted = false; : for(vector::iterator it = intervals.begin(); it< intervals.end
| l*******b 发帖数: 2586 | 4 in place没意义吧,这个是个vector的 | c*******u 发帖数: 47 | 5 我弱了,为什么vector 不能in place 呢,以为in place就是试用固定的,小的额外空
间。。。还有为什么大的过不了呢 | l*******b 发帖数: 2586 | 6 要在原数组上做,函数就不要返回值了呀。如果插入的区间基本随机,in place 一样
要写很多数据。vector 本身分配就是用了富余空间的,所以这个问题大约不在乎in
place 吧
【在 c*******u 的大作中提到】 : 我弱了,为什么vector 不能in place 呢,以为in place就是试用固定的,小的额外空 : 间。。。还有为什么大的过不了呢
| c*******u 发帖数: 47 | 7 恩,好像是的,谢谢!
【在 l*******b 的大作中提到】 : 要在原数组上做,函数就不要返回值了呀。如果插入的区间基本随机,in place 一样 : 要写很多数据。vector 本身分配就是用了富余空间的,所以这个问题大约不在乎in : place 吧
|
|