写的代码:
class Solution {
public:
string cc(int num){
string res="";
while(num>0){
int r=num % 2;
res=static_cast<char>(48+r)+res;
num/=2;
}
return res;
}
int minChanges(int n, int k) {
int res=0;
string n2=cc(n);
string k2=cc(k);
int n_size=n2.size();
int k_size=k2.size();
if(k_size>n_size){
return -1;
}else{
for(int i=0;i<k_size;i++){
if(n2[n_size-i-1]=='0' && k2[k_size-i-1]=='1')
return -1;
else if(n2[n_size-i-1]=='1' && k2[k_size-i-1]=='0')
res++;
}
}
for(int i=0;i<n_size-k_size;i++){
if(n2[i]=='1'){res++;}
}
return res;
}
};
数字转字符 static_cast<char> 或者用 to_string()也可以
标签:string,更改,int,res,3226,力扣,cc,num,size From: https://blog.csdn.net/m0_75015083/article/details/140604108