directory
Problem 1: Search for insertion location
Problem 2: Appearance arrays
Problem 3: maximum suborder sum
Problem 4: The length of the last word
Problem 5: Add one
Problem 6: binary summation
Problem 7: Find the square root
Problem 8: Climb the stairs
Delete duplicate elements from sorted lists
Merge two ordered arrays
LeetCode brushes the questions regularly, with 10 questions in each period. Comrades with heavy business can look at the ideas I share, which are not the most efficient solutions, but for mutual improvement.
Question 1:Search for insertion position
The requirements are as follows:
Answer (C language) :
int searchInsert(int* nums, int numsSize, int target){
int num=0;
if(numsSize<=0)
return 0;
for(int i=0,j=1; i<numsSize; i++,j++){if(target<nums[0]){
num=0;
break;
}
else if(target>nums[numsSize- 1]){
num=numsSize;
break;
}
else{
if(nums[i]==target) {
num=i;
break;
}
if(target>nums[i] && target<nums[j]){
num=j;
break; }}}return num;
}
Copy the code
Question 2:Appearance of the array
The requirements are as follows:
Answer (C language) :
char * countAndSay(int n){
if(n==1)
return "1";
char *s=countAndSay(n- 1);
char *a=(char *)malloc(5000);
char *ans=(char *)malloc(5000);
int count=0;
int l=strlen(s);
int j=0;
for(int i=0; i<l; i++){if(i == 0 || s[i]==s[i- 1])
count++;
else{
a[j++]='0'+count;
a[j++]=s[i- 1];
count=1;
}
if (i == l - 1) {
a[j++]='0'+count;
a[j++]=s[i];
}
}
a[j]='\ 0';
ans = a;
return ans;
}
Copy the code
Question 3:Maximum suborder sum
The requirements are as follows:
Answer (C language) :
int maxSubArray(int* nums, int numsSize){
int max = nums[0];
int b = 0;
for(int i = 0; i < numsSize; i++){
b += nums[i];
if(b > max) max = b;
if(b < 0) b = 0;
}
return max;
}
Copy the code
Problem 4: The length of the last word
The requirements are as follows:
Answer (C language) :
int lengthOfLastWord(char * s){
int num=strlen(s);
int len=0;
if(num<=0)
return 0;
// if there is a space, remove the space
// Return 0 if all Spaces are blank
while(s[--num]==' ') {if(num==0) {return 0; }}while(num>=0) {if(s[num--]==' ')
break;
len++;
}
return len;
}
Copy the code
Problem 5: Add one
The requirements are as follows:
Answer (C language) :
int* plusOne(int* digits, int digitsSize, int* returnSize){
int *buf=(int *)malloc((digitsSize + 1) * sizeof (int));
for(int i=digitsSize- 1; i>=0; i--){if(digits[i]+1= =10){
digits[i]=0;
}
else{
digits[i]+=1;
returnSize[0]=digitsSize;
return digits;
}
}
buf[0] =1;
for(int i=0,j=1; i<digitsSize; i++,j++) buf[j]=digits[i]; returnSize[0]=digitsSize+1;
return buf;
}
Copy the code
Problem 6: binary summation
The requirements are as follows:
Answer (C language) :
char * addBinary(char * a, char * b){
int num=0,len_a=strlen(a),len_b=strlen(b),len_long=0;
if(len_a>=len_b){
len_long=len_a+2;
}
else{
len_long=len_b+2;
}
char *buf=(char *)malloc(sizeof(char)*(len_long)) ;
buf[--len_long]='\ 0';
buf[0] ='0';
while(len_long>0){
num+=len_a>0? a[--len_a]-'0':0;
num+=len_b>0? b[--len_b]-'0':0;
if(num==0){
num=0;
buf[--len_long]='0';
}
else if(num==1){
num=0;
buf[--len_long]='1';
}
else if(num==2){
num=1;
buf[--len_long]='0';
}
else if(num==3){
num=1;
buf[--len_long]='1'; }}if(buf[0] = ='0')
return buf+1;
else
return buf;
}
Copy the code
Problem 7: Find the square root
The requirements are as follows:
Answer (C language) :
int mySqrt(int x){
// Newton iterative method
long r = x;
while(r * r > x)
{
r = (r + x / r) / 2;/ / iteration
}
return (int)r;
}
Copy the code
Problem 8: Climb the stairs
The requirements are as follows:
Answer (C language), algorithm description:
int climbStairs(int n){
if (n == 1) {
return 1;
}
int *dp=(int *)malloc(sizeof(int)*(n+1));
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
Copy the code
Delete duplicate elements from sorted lists
The requirements are as follows:
Answer (C language) :
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; *}; * /
struct ListNode* deleteDuplicates(struct ListNode* head){
struct ListNode * p = head;
struct ListNode * q;
while(NULL! = p) {while(NULL! = p->next && p->val == p->next->val) { q = p->next; p->next = q->next;free(q);
}
p = p->next;
}
return head;
}
Copy the code
Merge two ordered arrays
The requirements are as follows:
Answer (C language) :
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int p = m + n;
m--;
n--;
while (n >= 0 && m >= 0) {
nums1[--p] = nums1[m] <= nums2[n] ? nums2[n--] : nums1[m--];
}
while (n >= 0) { nums1[--p] = nums2[n--]; }}Copy the code