Teach you to understand LeetCode three questions

Insert picture description here

Series of articles catalog



Preface


Insert picture description here

1. The number that disappeared

1. Title description

The array nums contains all integers from 0 to n, but one is missing. Please write code to find the missing integer. Can you do it in O(n) time?
Insert picture description here

2. Problem solving ideas

There are two ways to solve this problem: the
first one:

1. Calculate the sum of numbers from 0 to n, sum1
2. Calculate the sum of all numbers in the array, sum2
3. Subtract sum2 from sum1 to get the missing numbers

The second type:
1. XOR the numbers from 0 to n.
2. Use the numbers in the x XOR array.
3. The result is that the missing number
codes are as follows (example):

#define _CRT_SECURE_NO_WARNINGS   1
#include<stdio.h>
int MissingNumber(int nums[], int sz) //解法1所以数之和减去少一个数的和,复杂度1-n那就是n
{
    
    
	int i = 0;
	int j = 0;
	int sum1 = 0;
	int sum2 = 0;
	int newlen = sz + 1;
	for (i = 0; i < newlen; i++) 
	{
    
    
		sum1 = sum1 + i;  //0到n的和
	}
	for (j = 0; j < sz; j++)
	{
    
    
		sum2 = sum2 + nums[j]; //数组的和
	}
	return sum1 - sum2; 
} 
int MissingNumber(int nums[], int sz) //解法2异或,复杂度n+n+1=2n+1那么复杂度就是n
{
    
    
	int x = 0;
	int newlen = sz + 1;
	int i = 0;
	for (i = 0; i < newlen; i++)
	{
    
    
		x = x^i;   
	}
	for (i = 0; i < sz; i++)
	{
    
    
		x = x^nums[i];
	}
	return x;
}
int main()
{
    
    
	int nums[] = {
    
    0,1, 2, 3, 4, 6, 7 };
	int sz = sizeof(nums) / sizeof(nums[0]);
	int digit = MissingNumber(nums, sz);
	printf("%d\n", digit);
}

Insert picture description here

Two, the frog jumps the stairs

1. Title description

A frog can jump up to one or two steps at a time. Find the total number of jumping methods the frog jumps on an n-level step.
Insert picture description here

2. Problem solving ideas

1. This problem can be similar to Fibonacci's number solution. Due to the low efficiency of the recursive solution algorithm, all can use the assignment method. Pay attention to the situation when n<2.
code show as below:

#define _CRT_SECURE_NO_WARNINGS   1
#include<stdio.h>
int Jump(int n)
{
    
    
	int a = 1;
	int b = 2;
	int c = a;
	if (n == 0)
	{
    
    
		return 1;
	}
	else if (n == 1)
	{
    
    
		return 1;
	}
	else if (n == 2)
	{
    
    
		return 2;
	}
	while (n > 2)  //赋值法解决效率比递归方法更高!
	{
    
    
		c = a + b;
		a = b;
		b = c;
		n--;
	}
	return c;
}
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	int sum = 0;
	sum = Jump(n);
	printf("%d\n", sum);
	return 0;
}

Insert picture description here

Third, the string is converted to an integer

1. Title description

Write a function StrToInt to realize the function of converting a string into an integer. You cannot use atoi or other similar library functions.

  1. First, the function discards useless beginning space characters as needed, until it finds the first non-space character.

  2. When the first non-blank character we find is a positive or negative sign, combine the sign with as many consecutive digits as possible behind it as the sign of the integer; if the first non-blank character is Number, it is directly combined with the following consecutive number characters to form an integer.

  3. In addition to the valid integer part of the string, there may also be extra characters, these characters can be ignored, they should not affect the function.

  4. Note: If the first non-space character in the string is not a valid integer character, the string is empty, or the string contains only blank characters, your function does not need to be converted.

  5. In any case, if the function cannot perform a valid conversion, please return 0.

Insert picture description here

2. Problem solving ideas

  1. Deal with spaces
  2. Determine the sign
  3. Handling signs
  4. Convert string to integer

code show as below:

#define _CRT_SECURE_NO_WARNINGS   1
#include<stdio.h>
#include<assert.h>
int change(char* str)
{
    
    
	assert(str);
	int signal = 1;
	int ret = 0;
	while (*str == ' ')
	{
    
    
		str++;
	}
	if (*str == '-')
	{
    
    
		signal = -1;
	}
	if (*str == '-' || *str == '+')
	{
    
    
		str++;
	}
	if (*str <'0'||*str>'9' )
	{
    
    
		return 0;
	}
	while (*str >= '0'&&*str <= '9')
	{
    
    
		ret = ret * 10 + *str - '0';
		str++;
	}
	ret = ret*signal;
	return ret;
}
int main()
{
    
    
	char a[] = "-30";
	char b[] = "20";
	int ret = change(a) + change(b);
	printf("%d\n", ret);
	return 0;
}

Insert picture description here


to sum up

The above are the three topics to be talked about today. This article only briefly introduces the solutions to the three simple topics of LeetCode, which we must master. In addition, if you have any questions above, please understand me, but it’s okay, mainly because I can persist, and I hope that some students who study together can help me correct it, but if you can, please be gentle and tell me that love and peace are forever Theme, love everyone.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44918090/article/details/115322457