table of Contents
Question 5: The first wrong version
Question 8: seeking 1 + 2 + ... + n
Item 9: 0 value becomes the number of steps
Question 10: How much less than the current number of digits
Stay button (LeetCode) regularly brush title, each lasting 10 questions, heavy traffic comrades can look at ideas I share, is not the most efficient solution, but only to enhance each other.
Question 1: duplicate element
Questions requirements are as follows:
Answer (C language):
int compare(const void *a, const void *b){
return *(int*)a - *(int*)b;
}
bool containsDuplicate(int* nums, int numsSize){
qsort(nums, numsSize, sizeof(int), compare);
for(int i = 0; i < numsSize - 1; i++){
if(nums[i] == nums[i+1]){
return true;
}
}
return false;
}
Operating efficiency as follows:
Problem 2: The power of two
Questions requirements are as follows:
Answer (C language):
bool isPowerOfTwo(int n){
while(n%2==0&&n>1){
n=n/2;
}
if(n==1){
return true;
}
else{
return false;
}
}
Operating efficiency as follows:
Question 3: Move the zero
Questions requirements are as follows:
Answer (C language):
void moveZeroes(int* nums, int numsSize){
for(int i=0,j = 0,temp=0; j < numsSize;++j) {
if(nums[j] != 0) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
++i;
}
}
}
Operating efficiency as follows:
Question 4: missing numbers
Questions requirements are as follows:
Answer (C language):
int missingNumber(int* nums, int numsSize){
int ans = numsSize*(numsSize + 1)/2, i;
for(i = 0; i < numsSize; i++){
ans -= nums[i];
}
return ans;
}
Operating efficiency as follows:
Question 5: The first wrong version
Questions requirements are as follows:
Answer (C language):
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
int firstBadVersion(int n) {
long low=1;
long high=n;
while(low<high){
long mid=(low+high)/2;
if(!isBadVersion(mid)){
low=mid+1;
}
else{
high=mid;
}
}
return low;
}
Operating efficiency as follows:
Question 6: masseur
Questions requirements are as follows:
Answer (C language):
//设dp[i]表示第i个房屋之前的最高金额,则求dp[numsSize-1]即可。
//状态转移方程:
//dp[0] = nums[0]
//dp[1] = max(nums[1], nums[0])
//dp[i] = max(dp[i-2] + nums[i], dp[i-1])(i>=2)
int massage(int* nums, int numsSize){
if(!nums || numsSize == 0)
return 0;
if(numsSize == 1)
return nums[0];
int *dp = (int*)malloc(sizeof(int) * numsSize);
dp[0] = nums[0];
dp[1] = fmax(nums[1], nums[0]);
for(int i = 2; i < numsSize; i++){
dp[i] = fmax(dp[i-2] + nums[i], dp[i-1]);
}
return dp[numsSize - 1];
}
Operating efficiency as follows:
Question 7: 3 power
Questions requirements are as follows:
Answer (C language):
bool isPowerOfThree(int n){
if (n == 0)
return false;
while(n%3==0){
n/=3;
}
if(n==1)
return true;
return false;
}
Operating efficiency as follows:
Question 8: seeking 1 + 2 + ... + n
Questions requirements are as follows:
Answer (C language):
//(1)如果多个变量均非0(包括None、False等),那么返回最后一个变量的值。如3 and 2 and 'a'的返回值为'a';
//(2)如果多个变量中存在0值,则返回第一个0值。如1 and 'a' and 0 and None的返回值为0。
int sumNums(int n){
int res = n;
n && (res += sumNums(n-1));
return res;
}
Operating efficiency as follows:
Item 9: 0 value becomes the number of steps
Questions requirements are as follows:
Answer (C language):
int numberOfSteps (int num){
int cou=0;
while(num!=0){
if(num%2==0){
num/=2;
}
else{
num-=1;
}
cou++;
}
return cou;
}
Operating efficiency as follows:
Question 10: How much less than the current number of digits
Questions requirements are as follows:
Answer (C language):
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
int* data_buf=(int*)malloc(sizeof(int)*(numsSize+1));
memset(data_buf,'\0',sizeof(int)*(numsSize+1));
for(int i=0;i<numsSize;i++){
for(int j=0;j<numsSize;j++){
if(nums[j]<nums[i])
data_buf[i]++;
}
}
data_buf[numsSize]='\0';
*returnSize=numsSize;
return data_buf;
}
Operating efficiency as follows: