C language curve overtaking must do a good collection of questions (programming question 2)

Foreword:

If you want to learn programming well, you need to learn more questions. We not only need to do more questions, but also do well! For this reason, I started a series of must-do questions for cornering and overtaking. This is the second programming question, each with about 5 questions. This series will be updated from time to time, so stay tuned!


1. Self-divisor

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int isSelfDividing(int num) {
	if (num < 1)
	{
		return 0;
	}
	int tmp = num;
	while (tmp)
	{
		int ret = tmp % 10;
		if (ret == 0 || num % ret != 0)
		{
			return 0;
		}
		tmp = tmp / 10;
	}
	return 1;
}

int* selfDividingNumbers(int left, int right, int* returnSize) {
	int pos = 0, i = 0;
	int* arr = (int*)malloc(sizeof(int) * (right - left + 1));
	for (i = left; i <= right; i++)
	{
		if (isSelfDividing(i))
		{
			arr[pos++] = i;
		}
	}
	*returnSize = pos;
	return arr;
}

 [Answer analysis]:

For the judgment of self-divisor, each digit of the number can be divisible by the source number. With this rule, we only need to loop to obtain each digit of a number, and then take the modulus with the source number to determine whether it is 0. If there is any one in the middle If it is not 0 , it means that it is not a divisor. Next, you only need to traverse each element in the array to determine whether it is a self-divisor, and if so, add it to the returned array.


2. The product of an array other than itself

Product of arrays except itself https://leetcode.cn/problems/product-of-array-except-self/

 

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
	int* arr = (int*)malloc(sizeof(int) *( numsSize));
	int i = 0, t = 1;
	arr[0] = 1;
	for (i = 1; i <= numsSize-1; i++)
	{
		arr[i] = arr[i - 1] * nums[i - 1];
	}
	for (i = numsSize-1; i >=0; i--)
	{
		arr[i] = arr[i] * t;
		t = t * nums[i ];
	}
	*returnSize = numsSize;
	return arr;
}

 Parse:

Method: left and right product list method


3. Addition without addition, subtraction, multiplication and division

Method 1: auto-increment 

The main idea: loop one of the values, decrement by 1 each time, and let the other value increase by itself

int Add(int num1, int num2) {
    if (num1 > 0)
    {
        while (num1--> 0)
        {
            num2++;
        }

    }
    else if (num1 < 0)
    {
        while (num1++ < 0)
        {
            num2--;
        }
    }
    return num2;
}
 Method 2: Bit arithmetic

summary: 

int Add(int num1, int num2) {
    int sum = num1 ^ num2, c = (num1 & num2) << 1;
    return sum + c;
}


4. Find all missing numbers in the array

Find all numbers that disappeared in an array https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array/

 

Method: in-place modification method
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){
  
   int i,t=0;
   int*p= (int*)malloc(numsSize*sizeof(int));
   for( i=0;i<numsSize;i++)
   {
       if(nums[abs(nums[i])-1]>0)          //原地标记数组元素,记得加abs
                nums[abs(nums[i])-1] *= -1;
   }
   for( i=0;i<numsSize;i++)
   {
       if( nums[i]>0)
            p[t++]=i+1;
   }
   *returnSize=t;
   return p;
}
[Answer analysis]:
An array of numsSize size, where the data of each element is within the interval [1, numsSize] , the solution is actually not complicated. Use the absolute value of the array element as the subscript, and set the data at the corresponding position to a negative number, such as position 0 If it is 3 , reset the data at position 3 to a negative value. After the reset of the array traversal is complete, only the position corresponding to the missing number will remain positive, and the positions of other numbers that have appeared will be negative numbers. Be careful not to repeat the setting Negative numbers, because negatives make positives.


5. The maximum number of consecutive 1s

The maximum number of consecutive ones icon-default.png?t=N6B9https://leetcode.cn/problems/max-consecutive-ones/

int Max(int x, int y)
{
	return x > y ? x : y;
}

int findMaxConsecutiveOnes(int* nums, int numsSize) {
	int count = 0, maxcount = 0,i=0;
	for (i = 0; i < numsSize; i++)
	{
		if (nums[i] == 1)
		{
			count++;
		}
		else
		{
			maxcount = Max(maxcount, count);
			count = 0;
		}
	}
	maxcount = Max(maxcount, count);
	return maxcount;
}

 Parse:

 


The above is my personal sharing, if you have any questions, welcome to discuss! !

I've seen this, why don't you pay attention and give a free like 

 

Guess you like

Origin blog.csdn.net/WHabc2002/article/details/132504176