classSolution{public:
vector<int>missingTwo(vector<int>& nums){sort(nums.begin(), nums.end());bool flag1 =false;bool flag2 =false;int num1 =-1, num2 =-1;int n = nums.size();for(int i =1; i <= n; i++){// cout << nums[i - 1] << " " << i + flag1 + flag2 << endl;if(nums[i -1]== i + flag1 + flag2){continue;}elseif(!flag1){
flag1 =true;
num1 = i;if(flag1 + i == nums[i -1])continue;
flag2 =true;
num2 = i +1;break;}elseif(!flag2){
flag2 =true;
num2 = nums[i -1]-1;}elsebreak;}if(!flag1) num1 = n +1;if(!flag2) num2 = n +2;return{num1, num2};}};
1016. 子串能表示从 1 到 N 数字的二进制串
代码实现(范围判断)
classSolution{public:boolqueryString(string s,int n){if(n >=2048)returnfalse;int i = n;int len =0;while(i){
i >>=1;
len++;}
set<int> cnt;for(int i =0; i < s.size(); i++){if(s[i]=='0')continue;int num =1;
cnt.emplace(num);for(int j = i +1; j < i + len && j < s.size(); j++){
num <<=1;
num +=(s[j]=='1');if(num <= n) cnt.emplace(num);}}return cnt.size()== n;}};