Help me out in this
yeah that only.
basically u r generating string level by level (which is bfs )
but it is showing compile error, so do u want to correct the compile error or what?
yeah correcting them pls wait
check now ->
#include <iostream>
class Solution {
public:
vector<string> v;
char ans;
int n,k;
int level=0;
void lev(string s,int level)
{ string temp="";
if(level==n)
{
ans=s[k-1];
return;
}
if(s.size()==0)
{
s=s+"0";
temp=s; //added
}
else
{
// string temp=""; declare out the blocks
for(int i=0;i<s.size();i++)
{
if(s.at(i)=='0')// u were using double quotes
{
temp=temp+"0"+"1";
}
if(s[i]=='1')
{
temp=temp+"1"+"0" ;
}
}
level++;
}
lev(temp,level);
}
int kthGrammar(int N, int K) {
n=N;
k=K;// u were using k=k
lev("",0);
int num=ans-'0';// leetcode is not able to recognise atoi(its c function)
return num;
}
};
no why stoi ?
stoi is to convert string to int. we have char.
check how i converted char to int (i substracted ‘0’ from the char , it will convert any numberic char to int )
for example-> ‘3’-‘0’ = 3 , ‘1’-‘0’=1 etc
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.