Why does some solution work on VSCode but not LeetCode?
Question:
I tried to resolve this Leetcode problem with the solution as follows on Visual Studio Code :
class CustomStack:
vals = list()
def __init__(self, maxSize):
self.maxSize = maxSize
def push(self, x):
if len(self.vals) < self.maxSize:
self.vals.append(x)
print(self.vals)
def pop(self):
if len(self.vals) > 0:
val = self.vals.pop()
print(val)
return val
else:
print(-1)
return -1
def increment(self, k, val):
k = min(k, len(self.vals))
for i in range(k):
self.vals[i] += val
print(self.vals)
It prints correct numbers on VSCode: [null,null,34,null,-1,null,null,63,null,null,null,null]
But it fails on LeetCode which prints [null,null,85,null,181,null,null,196,null,null,null,null]
I know the correct solution can be this:
class CustomStack:
def __init__(self, maxSize):
self.vals =[]
self.maxSize = maxSize
But why the original version works on VSCode, not Leetcode?
Answers:
When running your program in VS Code, you are testing a single example using one instance of the CustomStack
class. In LeetCode, multiple test cases are executed using a new instance of the CustomStack
class. Since vars
is a class variable, not an instance variable, it is being shared across all of the CustomStack
instances created during execution of your program. This means that each instance modifies the same list
.
You have already shown that the correct way to implement a variable specific to an instance of a class is by defining it during the __init__
method:
class CustomStack:
def __init__(self, maxSize):
self.vals =[]
self.maxSize = maxSize
I tried to resolve this Leetcode problem with the solution as follows on Visual Studio Code :
class CustomStack:
vals = list()
def __init__(self, maxSize):
self.maxSize = maxSize
def push(self, x):
if len(self.vals) < self.maxSize:
self.vals.append(x)
print(self.vals)
def pop(self):
if len(self.vals) > 0:
val = self.vals.pop()
print(val)
return val
else:
print(-1)
return -1
def increment(self, k, val):
k = min(k, len(self.vals))
for i in range(k):
self.vals[i] += val
print(self.vals)
It prints correct numbers on VSCode: [null,null,34,null,-1,null,null,63,null,null,null,null]
But it fails on LeetCode which prints [null,null,85,null,181,null,null,196,null,null,null,null]
I know the correct solution can be this:
class CustomStack:
def __init__(self, maxSize):
self.vals =[]
self.maxSize = maxSize
But why the original version works on VSCode, not Leetcode?
When running your program in VS Code, you are testing a single example using one instance of the CustomStack
class. In LeetCode, multiple test cases are executed using a new instance of the CustomStack
class. Since vars
is a class variable, not an instance variable, it is being shared across all of the CustomStack
instances created during execution of your program. This means that each instance modifies the same list
.
You have already shown that the correct way to implement a variable specific to an instance of a class is by defining it during the __init__
method:
class CustomStack:
def __init__(self, maxSize):
self.vals =[]
self.maxSize = maxSize