Article catalog
catalogue
1, The number of statistical digits is even
2, A single element in an ordered array
3, Adjust the array order so that odd numbers precede even numbers
Zero, write in front
This chapter is just a review: this chapter mainly describes the methods of exhaustive search
[question 27] given an array of n elements and x, find the subscript | exhaustive method of x in the array_ Where do heroes come from - CSDN blog difficulty: ★★☆☆☆, O(1) or O(n)https://blog.csdn.net/WhereIsHeroFrom/article/details/118273228 I will open a trial reading article every day. If I insist on punching in every day, I can go whoring all the time
1, The number of statistical digits is even
1. Title
Give you an array of integers nums, please return the median even numbers Number of numbers.
Example 1:
Input: num = [12345,2,67896]
Output: 2
Explanation:
12 is 2 digits (even digits)
345 is a 3-digit number (odd digits)
2 is a 1-digit number (odd digits)
6 is 1 digit (odd digits)
7896 is 4 digits (even digits)
Therefore, only 12 and 7896 are even digits
Force bucklehttps://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/
2. Problem solving
Idea: exhaustive It's over
bool judge(int a){ // Judge whether the input number is even, because the range of numbers is given, just list it directly if(a>=1&&a<10){ return 0; } if(a>=10&&a<100){ return 1; } if(a>=100&&a<1000){ return 0; } if(a>=1000&&a<10000){ return 1; } if(a>=10000&&a<100000){ return 0; } if(a==100000){ return 1; } return 0; } int findNumbers(int* nums, int numsSize){ int i,ans=0; for(i=0;i<numsSize;++i){ //Enumerations starting from 0 are even bits++ if(judge(nums[i])){ ans++; } } return ans; //Return results
3. Results
2, A single element in an ordered array
1. Title
Given an ordered array containing only integers, each element will appear twice, and only one number will appear once. Find out the number.
Example 1:
Input: num = [1,1,2,3,3,4,4,8,8]
Output: 2
Force bucklehttps://leetcode-cn.com/problems/single-element-in-a-sorted-array/
2. Problem solving
int singleNonDuplicate(int* nums, int numsSize){ int has[100001]={0}; //Define and initialize arrays int i; for(i=0;i<numsSize;++i){ has[nums[i]]++; //Enumerate each number. If it occurs twice, it will be + + twice, and if it occurs once, it will be + + once, which is 1 } for(i=0;i<numsSize;i++){ //Then exhaustive has finds the number of times 1 if(has[nums[i]]==1){ return nums[i]; //Return results here } } return 0; //It's actually useless, but you can return anything without reporting an error }
3. Results
3, Adjust the array order so that odd numbers precede even numbers
1. Title
Enter an integer array and implement a function to adjust the order of numbers in the array so that all odd numbers are in the first half of the array and all even numbers are in the second half of the array.
Example:
Input: nums= [1,2,3,4]
Output: [1,3,2,4]
Note: [3,1,2,4] is also one of the correct answers.
Force bucklehttps://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/ 2. Problem solving
int Qua(int x) { return x % 2; //Remainder parity for 2 } int cmp(const void *a, const void *b) { //Use library functions to design cmp return Qua(*(int *)b) - Qua(*(int *)a); //We need to put the big row after the remainder in front, so a and b change positions } int* exchange(int* nums, int numsSize, int* returnSize){ int i; int *ret = (int *)malloc( sizeof(int) * numsSize ); for(i = 0; i < numsSize; ++i) { ret[i] = nums[i]; } qsort(ret, numsSize, sizeof(int), cmp); *returnSize = numsSize; //Return array length return ret;//Return array }
3. Results