table of Contents
Problem 1: Search Insert location
Question 3: and maximum subsequence
Question 4: The length of the last word
Question 9: remove sorts the list of repeating elements
Question 10: Merge two ordered arrays
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.
Development: stay button (LeetCode) brush questions, simple questions (the first phase)
Problem 1: Search Insert location
Questions 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;
}
Problem 2: the appearance of an array
Questions 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;
}
Question 3: The maximum subsequence and
Questions 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;
}
Question 4: The length of the last word
Questions requirements are as follows:
Answer (C language):
int lengthOfLastWord(char * s){
int num=strlen(s);
int len=0;
if(num<=0)
return 0;
//重后往前,若有空格,则去掉空格
//若全为空格则返回0
while(s[--num]==' '){
if(num==0){
return 0;
}
}
while(num>=0){
if(s[num--]==' ')
break;
len++;
}
return len;
}
Question 5: plus one
Questions 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;
}
Question 6: Binary sum
Questions 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;
}
Question 7: square root
Questions requirements are as follows:
Answer (C language):
int mySqrt(int x){
//牛顿迭代法
long r = x;
while(r * r > x)
{
r = (r + x / r) / 2;//迭代
}
return (int)r;
}
Question 8: climbing stairs
Questions 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];
}
Question 9: remove sorts the list of repeating elements
Questions 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;
}
Question 10: Merge two ordered arrays
Questions 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--];
}
}