C++ 超出内存限制但在 Python 中则不然
我正在研究问题 1249。 LeetCode 上最少删除以形成有效括号。这是一个简单的问题,我可以用 C++ 和 Python 中的堆栈来解决。但是,我的 C++ 版本代码导致超出内存限制,而我的完全相同的 Python 代码仅消耗 15.3 MB 内存。谁能帮助我理解出了什么问题吗?
C++代码:
class Solution {
public:
string minRemoveToMakeValid(string s) {
stack<pair<char, int>> stack;
for (int i=0; i<s.size();i++){
if (s[i]!='(' && s[i]!=')')
continue;
else{
if (!(stack.empty()) && stack.top().first=='(' && s[i]==')')
stack.pop();
else
stack.push({s[i],i});
}
}
string res;
for (int i=s.size()-1; i>=0;i--){
if (!stack.empty() && i==stack.top().second)
stack.pop();
else{
res = s[i] + res;
}
}
return res;
}
};
即使将C++中的堆栈数据结构替换为向量,仍然会导致超出内存限制。
Python3
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
stack = []
for i, c in enumerate(s):
if c!="(" and c!=")":
continue
if stack and stack[-1][0] == '(' and c==")":
stack.pop()
else:
stack.append([c,i])
res =""
for i in range(len(s)-1,-1,-1):
if stack and i == stack[-1][1]:
stack.pop()
else:
res = s[i] + res
return res
I am working on question 1249. Minimum Remove to Make Valid Parentheses on LeetCode. It is an easy question which I can solve with stack in both C++ and Python. However, my C++ version code leads to Memory Limit Exceeded, while my exact same Python code only consumes 15.3 MB memory. Could anyone help me understand what is wrong, please?
C++ code:
class Solution {
public:
string minRemoveToMakeValid(string s) {
stack<pair<char, int>> stack;
for (int i=0; i<s.size();i++){
if (s[i]!='(' && s[i]!=')')
continue;
else{
if (!(stack.empty()) && stack.top().first=='(' && s[i]==')')
stack.pop();
else
stack.push({s[i],i});
}
}
string res;
for (int i=s.size()-1; i>=0;i--){
if (!stack.empty() && i==stack.top().second)
stack.pop();
else{
res = s[i] + res;
}
}
return res;
}
};
Even if replace stack data structure in C++ with vector, it still leads to Memory Limit Exceeded.
Python3
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
stack = []
for i, c in enumerate(s):
if c!="(" and c!=")":
continue
if stack and stack[-1][0] == '(' and c==")":
stack.pop()
else:
stack.append([c,i])
res =""
for i in range(len(s)-1,-1,-1):
if stack and i == stack[-1][1]:
stack.pop()
else:
res = s[i] + res
return res
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是关于什么可以减少内存占用的另一种猜测。
我猜想每个
res = s[i] + res;
可能会重复重新分配内存,导致res
过度分配超过所需的内存。下面的代码(我希望)进行的分配要少得多,每个分配的大小都是当时所需的精确大小:
This is another guess about what could reduce the memory footprint.
I guess that each
res = s[i] + res;
may repeatedly reallocate memory resulting inres
being over allocated above what is needed.The code below (I'm hoping) makes far fewer allocations, each of an exact size needed at the time: