dimanche 16 juillet 2023

How for loop with &&(and) condition works

On LeetCode a user posted his solution, but this line of code was pretty unclear to me, I tried to debug but could not understand it's working here is the piece of code.

string p = "c*a*b";
vector<int>dp(p.size()+1, 0);
for (int j = 0; j < p.size() && p[j] == '*'; ++j) {
         dp[j + 1] = 1;
     }

The output comes as [0 0 0 0 0 0 ]

whereas I guess the output should be

at j = 1, p[j] ==, so dp[1+1] =1
at j = 3, p[j] ==
, so dp[3+1] =1

and finally it should be [0 0 1 0 1 0 ]

Can someone explain me the working for the above for loop

Aucun commentaire:

Enregistrer un commentaire