Introduction to c language: 100 cases, 77475 questions

#Here is an integer array nums. Please find out the bitwise or possible maximum value of the nums subset, and return the number of different non empty subsets that can get the maximum value
#For an N element, the number of subsets is 2^n, and the number of non empty subsets is 2^n-1

int countMaxOrSubsets(int *nums,int n){
int i,j;
int ans=0;
int max=0,cnt=0;
//1<n<2^n
for(i=0;i<(1<<n);i++){
ans=0;
for(j=0;j<n;j++){
if((1<<j)&i)
//I & (1 < < J) represents the number obtained by bitwise sum of I and 1 < < J (i.e. 2^j). The binary representation of 1 < < J means that only the number on the jth position (counting from right to left, starting from 0) is 1, and the number on the other positions is 0. When I and 1 < < J perform bitwise and operation, the jth position of I is 1 and returns 1 < < J (true in the judgment statement), and the jth position of I is 0 and returns 0 (false in the judgment statement)
//Specifically,

//When i=0, any j is not satisfied.

//When i=1, j=0 meets the condition.

//When i=2, j=1 meets the condition.

//When i=3, j=0,1 meets the condition.

//When i=4, j=2 meets the condition.

//When i=5, j=0,2 meets the condition.

//When i=6, j=1,2 meets the condition.

//When i=7, j=0,1,2 meets the condition.

//When i=8, j=3 meets the condition.


//When i=2^n-1, j=0,1,2,...,n-1 satisfies the condition.

//To sum up, if j represents the index of array a (length n), and the loop structure is as follows, it means that a sub array of array A is selected in turn for operation until it is selected.
{
ans|=nums[j];//Perform or operate on a subset

}
if(ans>max){
max=ans;
cnt=1;
}else if(ans==max){
cnt++;
}
}
return cnt;
}

#Implement a function. Give you an integer array digits, where each element is a number (0 - 9). There may be duplicate elements in the array. You need to find all integers that meet the following conditions and are different from each other:     this integer is composed of three elements in digits connected in any order. The integer does not contain leading zeros. This integer is an even number. Arrange all the integers that are different from each other in ascending order and return them as an array.

int *findEvenNumbers(int *digits,int digitsSize,int *retrunSize){
int hash[10];
int *ret=(*int)malloc(sizeof(int)*1000);
*returnSize=0;
int i,j,k;
memset(hash,o,sizeof(hash));
for(i=0;i<digitsSize;i++){
hash[digit[i]]++;
}
for(i=1;i<=9;i++){
for(j=1;j<=9;9++){
for(k=0;j<=9;k++){
hash[i]--;hash[j]--;hash[k]--;
if(hash[i]>0&&hash[j]>0&&hash[k]>0){
ret[(*returnSize)++]=i*100+j*10+k;
}
hash[i]++;hash[j]++;hash[k]++;

}
}
}
return ret;

}

#Implement a myAtoi(string s) function to convert a string into a 32-bit signed integer (similar to the atoi function in C/C++). The algorithm of the function myAtoi(string s) is as follows:   
1) Read in the string and discard useless leading spaces;   
2) Check whether the next character is positive or negative, and read the character. Determine whether the final result is negative or positive. If neither exists, the result is assumed to be positive.   
3) Read the next character until you reach the next non numeric character or the end of the input. The rest of the string will be ignored. Convert the numbers read in the previous steps into integers. If no numbers are read in, the integer is 0
4) If the integer exceeds the range of 32-bit signed integers [− 2 ^ {31}, 2 ^ {31} − 1], you need to truncate this integer to keep it within this range. Specifically, integers less than − 2 ^ {31} should be fixed as − 2 ^ {31}, and integers greater than 2 ^ {31} should be fixed as 2 ^ {31} − 1.   
Returns an integer as the final result.   
Sample input 1: "+123"    
Sample output 1:123


Sample input 2: "   -123"   
Sample output 2:-123 − 123


Sample input 3: "hahaha 996"   
Sample output 3:0


Example input 4: "-92312432833324473242332",  
Sample output 4:-2147483648


Sample input 5: "- +1"   
Sample output 5:0

class solution{
public:
string trim(string s){//Returns a string without spaces
string ret;
int notEmptyIdx=s.size();//length
for(int i=0;i<s.size();i++){
if(s[i]==" "){
continue;
}else{
notEmptyIdx=i;
break;
}
for(int i=notEmptyIdx;i<s.size();i++){
ret.push_back(s[i]);//Function to insert elements later
}
return ret;
}
}
string postiveChect(string s){//Remove the first as+
string ret;
for(int i=1;i<s.size();i++){
ret.push_back[s[i]];
}
return ret;
}
string negativeSplit(string s,int &flag){
if(s.size()==0){
flag=1;
return s;
}
if(s[0]='-'){
flag=-1;
string ret;
for(int i=1;i<s.size();i++){
ret.push_back(s[i]);
}
return ret;
}
flag=1;
return s;
}
}
int myAtoi(string s){
int flag=1;
long long sum=0;
s=trim(s);//Remove Spaces 
if(s.size()==0){
return 0;
}
if(s[0]=='+'){
s=positiveCheck(s);
}else if(s[0]='-'){
s=negativeSplit(s,flag);
}
long long minv = INT_MIN;
long long maxv = INT_MAX;
for(int i=0;i<s.size();i++){
if(s[i]>='0'&&s[i]<='9'){
sum=sum*10+s[i]-'0';//Why multiply 10 here
if(sum * flag < minv) {
return minv;                      
}
if(sum * flag > maxv) {
return maxv;        
}else{//I feel there is a problem in judging illegal strings here
break;
}
}
return sum*flag;
}
}

Tags: C

Posted by dave914 on Wed, 03 Aug 2022 02:27:47 +0930