Cannot understand this behaviour of java when adding a list to a list of lists.
https://leetcode.com/problems/subsets/submissions/1236349921
this is the leetcode ques i was solving
as you can see the ans list which is supposed to be a subset of the set is being formed and is printed in the stdout just before it was added to the fin global string which is ultimately returned as the answer.
on further debugging its adding the current string n number of times where n is the array index of the power set array
so in the last iteration its adding a null set n(8) times.
But when i pass a clone of the list in function call, the program works as intended
Any explanation?
66 Replies
β
This post has been reserved for your question.
Hey @lucifer7303! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
code?
π€
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Its in the link
Its a leetcode submission with the code, the stdout and the return value
No, it says "submission does not exist"
https://leetcode.com/problems/subsets/submissions/1236349921
sorry, it mustve expired, updated the link
Same result. I'm not a leetcode expert, but maybe it doesn't allow you to share code
wait lemme share a screenshot
yeah, no
Computers are capable of copying text. Do that instead
This message has been formatted automatically. You can disable this using
/preferences
.i remember there being a rule for not posting the whole code somewhere in the server, maybe im confused with the rules of another server
the subset() function is called by the main leetcode driver code
It's not so much a rule as a limitation of discord, and it's not like a screenshot would do anything better
the input,output(return of the subset() function) and the stdout
i tried taking the fin ans list as a parameter for the helper function but that doesnt help
as you can see the ans list is being formed and printed but when i do fin.add(ans) it just adds it n number of times, with n being its (index+1) in the fin list
Obviously add() is called more than once
what?
doesnt it add at the end of the list
You said that when you call add(), it adds n number of times
im adding a list at the end of a list of lists
No, it's just that add() was called n times
but how
the prob and confusing part is
when i pass the clone of the ans list
then this code works perfectly
Well add() is part of your finishing logic of the recursion
I'm not sure why that's weird. You're supposed to do exactly that
This message has been formatted automatically. You can disable this using
/preferences
.this little difference in the function call of helper
fixes the code
but why cant i pass the original list
it worked in c++ when a friend of mine did it
.... Because you're constantly modifying it
he didnt pass its clone
I imagine he used a list class directly rather than a pointer to a list class
iirc no, i copied the code almost the same to replicate the code
So what? They're different languages, and notably C++ does have stuff like pointers
im weak at pointers so maybe i didnt understand the actual happening
C++ with typically copy objects constantly rather than point to them, unless you tell it otherwise
but the thing is the ans list is being formed correctly in the stdout just before adding it to fin
im talking about the stdout here
its not like the ans list(the first parameter) isnt being formed correctly
Because stdout copies the current content of the list and prints it right here right now. It doesn't matter that you change the list later
so its a problem with my oops basics?
can you explain it in a lil simpler terms why passing clone of the ans list works but not when i pass the object itself
go through how recursion is going on.
i dry ran it, the logic is okay
its a problem with passing by value or reference i think
rather i should say passing a clone or the object itself
You're constantly modifying that list, making it so when you're done with it it is empty.
So if you add that list directly, in the end you obtain empty lists, and if you add clones of it, in the end you obtain a bunch of lists that each are what the list was when you cloned it
there are two things i dont understand:
why is fin list being reset every function call, isnt it a global variable for that object
and why ans is added n number of times
fin is not reset. You screenshot shows that it is growing
lets put aside the n addtition of ans list, but shouldnt the fin list be [[1,2],[1,3],[1,3],[1],[1],[1]...]
I don't understand your question, you have duplicates
the fin list have duplicates, right?
and the fin list is a global variable so its value should be constant among various function calls
so why is it that the ans list is added to an empty fin list
shouldnt it be added to the modified fin list which was modified in a previous function
What?
look at the second line
How can a variable that you keep modifying be constant among various function calls?
it formed fin=[[1,2]]
and fin is a global variable
so the next time i print fin in a function which is called AFTER the execution of previous funnction, it should be [[1,2]]
but here its empty adn the ans list is added n times
So it's empty and it contains something n times
You really need to try and reevaluate how you want to explain the situation
For most people, something empty contains nothing
yh i dont think im gonna understand this over text
gotta consult a professor in my college
I still encourage you not to explain to them that it confuses you that your result that is empty contains n thing
maybe i should dry run again
no i mean
wait lemme dry run and ill reply
btw what i meant was why was fin reset everytime ans is being added to it
like a function added [1,2] to the fin list
but when the next function adds [1] to it
it becomes [1],[1],[1] and not [1,2],[1],[1],[1]
im adding duplicates including the problem of the addition of ans n times
fin is not reset. It's just you're modifying the object you put inside it
i know it shouldnt have duplicates
run a debugger locally if you know how.
oh okay
i effing got it
my dumb a########
the addition and everything is absolutely fine but since in the end the same list is ended up as being empty
and since its the same object added everytime in the fin list
all of them are modified to empty
right?
yup
We've all been through that one. It takes time and thinking to get to the realization
thanks a lot kyo-chanπ π
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
but how do i tackle this prob
like isnt making a clone everytime
O(n)
isnt there a better way
and how does c++ do it??
like arent vector objects too?
C++ makes clones without you telling it to
oh so when i do not pass the object without &
it passes a clone,without any mention
well thats just plain stupid if thats the case
or maybe not, my basics are wonky in pointers. not c++'s fault
When you get used to languages that don't do that you tend to start thinking that C++ is idiotic for doing that. But at least it's consistent between objects and native types
so the only way of doing this ques by this approach is to pass the clone?
but that adds to the complexity of the question
like doing the cloning n times is O(n^2)
If you were allowed not to count the data sent to output as part of the complexity you could reduce, but there is no magic to how the answer expects a number of distinct sets
dont know if asking dsa ques is allowed in this channel
i mean by a programming perspective
theres no workaround to this prob,right
you gotta make a clone
Unknown Userβ’8mo ago
Message Not Public
Sign In & Join Server To View
π€
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.