LeetCode 11~20题C语言题解以及相应说明

分享LeetCode 11~20题C语言题解以及相应说明

说明:写的时间比较早了,有些函数可能需要重新适配

//20.即三种括号要配对,可利用动态数组设置一个堆栈,将下一个符号与堆栈进行对比
//Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
//An input string is valid if:
//Open brackets must be closed by the same type of brackets.
//Open brackets must be closed in the correct order.
//Note that an empty string is also considered valid.
//bool isValid(char* s) {
//    int count_small=0,count_big=0,count_middle=0;
//    int len_str=strlen(s);
//    char *stack = (char *)malloc(sizeof(char)*(len_str));//numRows行
//    int j=0;
//    if (len_str==0)
//    {
//        return true;
//    }else{
//        for (int i = 0; i < len_str; i++)
//        {
//            if (j==0)//堆栈为空,则入栈
//            {
//                stack[j]=s[i];
//                j++;
//            }else if(s[i]==')' && stack[j-1]=='('){//如果当前符号与栈顶匹配,则出栈
//                j--;
//            }else if(s[i]==']' && stack[j-1]=='['){
//                j--;
//            }else if(s[i]=='}' && stack[j-1]=='{'){
//                j--;
//            }else//其他情况则入栈
//            {
//                stack[j]=s[i];
//                j++;
//            }
//        }
//        if (j==0)//遍历了整个字符串之后,如果栈为空,表示匹配ok
//        {
//            return true;
//        }else
//        {
//            return false;
//        }
//    }
//}

//19.Given a linked list, remove the n-th node from the end of list and return its head.
//Given linked list: 1->2->3->4->5, and n = 2.
//After removing the second node from the end, the linked list becomes 1->2->3->5.
//Could you do this in one pass?
//struct ListNode {
//    int val;
//    struct ListNode *next;
//};
// 
//struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
//    struct ListNode* front;//前节点
//    struct ListNode* back;//后节点,与前节点保持n的距离
//    front=back=head;
//    if (front->next==NULL && n==1)//只有一个元素,并且n=1,则返回NULL
//    {
//        return NULL;
//    }else{
//        for (int i = 0; i < n; i++)
//        {
//            front=front->next; 
//        }
//        if(front==NULL){//前节点超过了最后一个有效元素,则表示需要去除第一个元素
//            head = head->next;
//        }else{
//            while ( front->next!=NULL)
//            {
//                front=front->next;
//                back=back->next; 
//            }
//            back->next = back->next->next;
//        }
//        return head;
//    }
//}

//18.Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
//Note:
//The solution set must not contain duplicate quadruplets.
//void swap(int s[],int i,int j)  
//{  
//    int temp;  
//    temp=s[i];  
//    s[i]=s[j];  
//    s[j]=temp;  
//}
//void QuickSort(int s[],int low,int high)  
//{  
//    int i;  
//    int last;       //记录基准的位置  
//    if(low<high)    //当数组中的元素个数大于1时,才进行操作  
//    {  
//        last=low;   //选取第一个元素作为基准  
//        //把小于基准元与大于基准元的分开,last记录它们分开的界限  
//        for(i=low+1;i<=high;i++)  
//        {  
//            if(s[i]<s[low])  
//            swap(s,++last,i);  
//        }  
//  
//        swap(s,last,low);//基准元与界限交换,这样的话,基准元两边就是一边大于,一边小于;  
//        QuickSort(s,low,last-1);  //对左区间递归排序  
//        QuickSort(s,last+1,high);//对右区间递归排序  
//    }  
//}  
//int** threeSum(int* nums, int numsSize, int* returnSize,int target) {
//    int coff=1;//(numsSize*(numsSize-1)*(numsSize-2)+5)/6;
//    int **tmp = (int **)malloc(sizeof(int *)*(coff));//numRows行
//    for (int i = 0; i < coff; i++)
//    {
//        tmp[i] = (int *)malloc(sizeof(int)*3);     //每行col列
//    }
//    int index=1;
//    int front;
//    int back;
//    //printf("numsSize=%d",numsSize);
//    
//    for (int i = 0;i < numsSize-2; i++)
//    {
//        //target = -nums[i];
//        front = i+1;
//        back = numsSize-1;
//        while (front<back)
//        {
//            int sum = target-(nums[i]+nums[front]+nums[back]);
//            if (!sum)
//            {
//                tmp = (int **)realloc(tmp,sizeof(int *)*(index));//numRows行
//                int index_tmp = index-1;
//                tmp[index_tmp] = (int *)malloc(sizeof(int)*3);     //每行col列
//                tmp[index_tmp][0] = nums[i];
//                tmp[index_tmp][1] = nums[front];
//                tmp[index_tmp][2] = nums[back];
//                index++;
//                while (front<back && nums[front]==nums[front+1])
//                {
//                    front++;
//                }
//                while (front<back && nums[back]==nums[back-1])
//                {
//                    back--;
//                }
//                front++; back--;
//            }else if(sum>0){
//                front++;
//            }else
//            {
//                back--;
//            }
//        }
//        while (nums[i]==nums[i+1])
//        {
//            i++;
//        }
//    }
//    *returnSize = index-1;
//    return tmp;
//}
//int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
//    int coff=1;//(numsSize*(numsSize-1)*(numsSize-2)+5)/6;
//    int **tmp = (int **)malloc(sizeof(int *)*(coff));//numRows行
//    int index=1;
//    int remain = 0;
//    int threeSum_Size=0;
//    int **c;
//    for (int i = 0; i < coff; i++)
//    {
//        tmp[i] = (int *)malloc(sizeof(int)*4);     //每行col列
//    }
//    QuickSort(nums,0,numsSize-1);
//    for (int i = 0; i < numsSize-3; i++)
//    {
//        
//        remain = target-nums[i];
//        c = threeSum(&nums[i+1],numsSize-i-1,&threeSum_Size,remain);
//        if (threeSum_Size)
//        {
//            *returnSize =*returnSize+threeSum_Size;
//            tmp = (int **)realloc(tmp,sizeof(int *)*(*returnSize));//numRows行
//            for (int k = (*returnSize)-threeSum_Size,j=0; k < (*returnSize); k++,j++)
//            {
//                tmp[k] = (int *)malloc(sizeof(int)*4);     //每行col列
//                tmp[k][0] = nums[i];
//                tmp[k][1] = c[j][0];
//                tmp[k][2] = c[j][1];
//                tmp[k][3] = c[j][2];
//            }
//
//        }
//        while (nums[i]==nums[i+1])
//        {
//            i++;
//        }        
//    }
//    return tmp;
//}

//17.Given a digit string, return all possible letter combinations that the number could represent.
//A mapping of digit to letters (just like on the telephone buttons) is given below.
//char** letterCombinations(char* digits, int* returnSize) {//先生成整个二维字符数组,然后再从第一个开始往里填相应的字符
//    int count=1;//(numsSize*(numsSize-1)*(numsSize-2)+5)/6;
//    int str_len = strlen(digits);
//    
//    if(str_len==0)
//    {
//        *returnSize = 0;
//        char **tmp = (char **)malloc(sizeof(char *)*(count));//numRows行
//        tmp[0] = (char *)malloc(sizeof(char)*(str_len));     //每行col列
//        return tmp;
//    }else {
//        for (int j = 0; j < str_len; j++)
//        {
//            if (digits[j]=='1'|| digits[j]=='0')
//            {
//                continue;
//            }else if (digits[j]=='9'|| digits[j]=='7'){
//                count *=4;
//            }else{
//                count *=3;
//            }
//        
//        }
//        * returnSize = count;//总共有多少行
//        int replace=1;//计算重复的次数
//        char **tmp = (char **)malloc(sizeof(char *)*(count));//numRows行
//        for (int i = 0; i < count; i++)
//        {
//            tmp[i] = (char *)malloc(sizeof(char)*(str_len+1));     //每行col列,多一列用于存放'\0'
//        }
//        for (int j = 0; j < str_len; j++)
//        {
//            if (digits[j]=='1'|| digits[j]=='0')
//            {
//                continue;
//            }else if (digits[j]=='9'){
//                int count_tmp = count;
//                count = count/4;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'w';//i*count_tmp表示跳了这么多行之后需要进行重复填写
//                        tmp[(count+k)+i*count_tmp][j] = 'x';
//                        tmp[2*count+k+i*count_tmp][j]= 'y';
//                        tmp[3*count+k+i*count_tmp][j]= 'z';
//                    }
//                }
//                replace=replace*4;
//            }else if (digits[j]=='2'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'a';
//                        tmp[(count+k)+i*count_tmp][j] = 'b';
//                        tmp[2*count+k+i*count_tmp][j]= 'c';
//                    }
//                }
//                replace=replace*3;
//                
//            }else if (digits[j]=='3'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'd';
//                        tmp[(count+k)+i*count_tmp][j] = 'e';
//                        tmp[2*count+k+i*count_tmp][j]= 'f';
//                    }
//                }
//                replace=replace*3;
//            }else if (digits[j]=='4'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'g';
//                        tmp[(count+k)+i*count_tmp][j] = 'h';
//                        tmp[2*count+k+i*count_tmp][j]= 'i';
//                    }
//                }
//                replace=replace*3;
//            }else if (digits[j]=='5'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'j';
//                        tmp[(count+k)+i*count_tmp][j] = 'k';
//                        tmp[2*count+k+i*count_tmp][j]= 'l';
//                    }
//                }
//                replace=replace*3;
//            }else if (digits[j]=='6'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'm';
//                        tmp[(count+k)+i*count_tmp][j] = 'n';
//                        tmp[2*count+k+i*count_tmp][j]= 'o';
//                    }
//                }
//                replace=replace*3;
//            }else if (digits[j]=='7'){
//                int count_tmp = count;
//                count = count/4;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 'p';
//                        tmp[(count+k)+i*count_tmp][j] = 'q';
//                        tmp[2*count+k+i*count_tmp][j]= 'r';
//                        tmp[3*count+k+i*count_tmp][j]= 's';
//                    }
//                }
//                replace=replace*4;
//            }else if (digits[j]=='8'){
//                int count_tmp = count;
//                count = count/3;
//                for (int i = 0; i < replace; i++)
//                {
//                    for (int k = 0; k < count; k++)
//                    {
//                        tmp[k+i*count_tmp][j] = 't';
//                        tmp[(count+k)+i*count_tmp][j] = 'u';
//                        tmp[2*count+k+i*count_tmp][j]= 'v';
//                    }
//                }
//                replace=replace*3;
//            }
//        }
//        for (int k = 0; k < * returnSize; k++)//进行最后的填'\0',即结束符
//        {
//            tmp[k][str_len] = '\0';
//        }
//        return tmp;
//    }

//16.Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
//    For example, given array S = {-1 2 1 -4}, and target = 1.
//    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
**********快速排序算法*****************/  
*交换数组中的两个元素*/  
//void swap(int s[],int i,int j)  
//{  
//    int temp;  
//    temp=s[i];  
//    s[i]=s[j];  
//    s[j]=temp;  
//}
//void QuickSort(int s[],int low,int high)  
//{  
//    int i;  
//    int last;       //记录基准的位置  
//    if(low<high)    //当数组中的元素个数大于1时,才进行操作  
//    {  
//        last=low;   //选取第一个元素作为基准  
//        //把小于基准元与大于基准元的分开,last记录它们分开的界限  
//        for(i=low+1;i<=high;i++)  
//        {  
//            if(s[i]<s[low])  
//            swap(s,++last,i);  
//        }  
//  
//        swap(s,last,low);//基准元与界限交换,这样的话,基准元两边就是一边大于,一边小于;  
//        QuickSort(s,low,last-1);  //对左区间递归排序  
//        QuickSort(s,last+1,high);//对右区间递归排序  
//    }  
//} 
//int threeSumClosest(int* nums, int numsSize, int target) {
//    int closest=nums[0]+nums[1]+nums[numsSize-1]-target;
//    int front;
//    int back;
//    int sum;
//    int flag=1;
//    //printf("numsSize=%d",numsSize);
//    QuickSort(nums,0,numsSize-1);
//    for (int i = 0;i < numsSize-2; i++)
//    {
//        front = i+1;
//        back = numsSize-1;
//        while (front<back)
//        {
//            sum = nums[i]+nums[front]+nums[back]-target;
//            if (abs(sum)<abs(closest))
//            {
//                if (sum==0)
//                {
//                    return target;
//                }else if(sum>0)
//                {
//                    closest = sum;
//                    flag = 1;
//                }else
//                {
//                    closest = -sum;
//                    flag = 0;
//                }
//            }
//            if (sum>0)
//            {
//                while (front<back && nums[back]==nums[back-1])
//                {
//                    back--;
//                }
//                back--;
//                
//            }else
//            {
//                while (front<back && nums[front]==nums[front+1])
//                {
//                    front++;
//                }
//                front++;
//                
//            }
//            
//            
//        }
//        while (nums[i]==nums[i+1])
//        {
//            i++;
//        }
//        
//
//    }
//    if (flag)
//    {
//        return closest+target;
//    }else
//    {
//        return target-closest;
//    }
//    
//}

//15.Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
//For example, given array S = [-1, 0, 1, 2, -1, -4],
//
//A solution set is:
//[
//  [-1, 0, 1],
//  [-1, -1, 2]
//]
/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
///**********快速排序算法*****************/  
///*交换数组中的两个元素*/  
//void swap(int s[],int i,int j)  
//{  
//    int temp;  
//    temp=s[i];  
//    s[i]=s[j];  
//    s[j]=temp;  
//}
//void QuickSort(int s[],int low,int high)  
//{  
//    int i;  
//    int last;       //记录基准的位置  
//    if(low<high)    //当数组中的元素个数大于1时,才进行操作  
//    {  
//        last=low;   //选取第一个元素作为基准  
//        //把小于基准元与大于基准元的分开,last记录它们分开的界限  
//        for(i=low+1;i<=high;i++)  
//        {  
//            if(s[i]<s[low])  
//            swap(s,++last,i);  
//        }  
//  
//        swap(s,last,low);//基准元与界限交换,这样的话,基准元两边就是一边大于,一边小于;  
//        QuickSort(s,low,last-1);  //对左区间递归排序  
//        QuickSort(s,last+1,high);//对右区间递归排序  
//    }  
//}  
//int** threeSum(int* nums, int numsSize, int* returnSize) {
//    int coff=1;//(numsSize*(numsSize-1)*(numsSize-2)+5)/6;
//    int **tmp = (int **)malloc(sizeof(int *)*(coff));//numRows行
//    for (int i = 0; i < coff; i++)
//    {
//        tmp[i] = (int *)malloc(sizeof(int)*3);     //每行col列
//        //memset(tmp[i],0,sizeof(tmp[i]));
//    }
//    int index=1;
//    int target;
//    int front;
//    int back;
//    //printf("numsSize=%d",numsSize);
//    QuickSort(nums,0,numsSize-1);
//    for (int i = 0;i < numsSize-2; i++)
//    {
//        //target = -nums[i];
//        front = i+1;
//        back = numsSize-1;
//        while (front<back)
//        {
//            int sum = nums[i]+nums[front]+nums[back];
//            if (!sum)
//            {
//                tmp = (int **)realloc(tmp,sizeof(int *)*(index));//numRows行
//                int index_tmp = index-1;
//                tmp[index_tmp] = (int *)malloc(sizeof(int)*3);     //每行col列
//                tmp[index_tmp][0] = nums[i];
//                tmp[index_tmp][1] = nums[front];
//                tmp[index_tmp][2] = nums[back];
//                index++;
//                while (front<back && nums[front]==nums[front+1])
//                {
//                    front++;
//                }
//                while (front<back && nums[back]==nums[back-1])
//                {
//                    back--;
//                }
//                front++; back--;
//            }else if(sum<0){
//                front++;
//            }else
//            {
//                back--;
//            }
//        }
//        while (nums[i]==nums[i+1])
//        {
//            i++;
//        }
//    }
//    * returnSize = index-1;
//    return tmp;
//}

//14.Write a function to find the longest common prefix string amongst an array of strings.
//char* longestCommonPrefix(char** strs, int strsSize) {
//    if(*strs==0)
//    {
//        return "";
//    }else if(strsSize==1){
//        return * strs;
//    }else{
//        int strlen1 = strlen(*strs);//第一个字符串的长度
//        int index=0;
//        char *p = (char *)malloc(strlen1+1);//char为一个字节,所以sizeof(char)=1
//        bool flag=true;
//        for (int j = 0; j < strlen1; j++)
//        {
//            for (int i = 1; i < strsSize; i++)
//            {
//                if (*(*strs+j)!=(*(*(strs+i)+j)))
//                {
//                    flag=false;
//                    break;
//                }
//            } 
//            if (!flag)
//            {
//                if (!index)
//                {
//                    return "";
//                }else
//                {
//                    memcpy(p,*strs,index);
//                    p[index]='\0';
//                    return p;
//                }
//               
//            }else
//            {
//                index++;
//            }
//        }
//        memcpy(p,*strs,index);
//        p[index]='\0';
//        return p;
//    }
//}

//13. 罗马字转为整数Roman to Integer
//Given a roman numeral, convert it to an integer.
//Input is guaranteed to be within the range from 1 to 3999.
//int romanToInt(char* s) {
//    int num=0;
//    while (*s)
//    {
//        switch (*s)
//        {
//        case 'M':
//            num = num+1000;
//            s++;
//            break;
//        case 'D':
//            num = num+500;
//            s++;
//            break;
//        case 'C':
//            if (*(s+1)=='M')
//            {
//                num = num+900;
//                s=s+2;
//            }else if (*(s+1)=='D')
//            {
//                num = num+400;
//                s=s+2;
//            }else
//            {
//                num = num+100;
//                s++;
//            }
//            break;
//        case 'L':
//            num = num+50;
//            s++;
//            break;
//        case 'X':
//            if (*(s+1)=='C')
//            {
//                num = num+90;
//                s=s+2;
//            }else if (*(s+1)=='L')
//            {
//                num = num+40;
//                s=s+2;
//            }else
//            {
//                num = num+10;
//                s++;
//            }
//            break;
//        case 'V':
//            num = num+5;
//            s++;
//            break;
//        case 'I':
//            if (*(s+1)=='X')
//            {
//                num = num+9;
//                s=s+2;
//            }else if (*(s+1)=='V')
//            {
//                num = num+4;
//                s=s+2;
//            }else
//            {
//                num = num+1;
//                s++;
//            }
//            break;
//        default:
//            return num;
//            break;
//        }
//    }
//    return num;
//}

//12. 数字转罗马字符Integer to Roman
//Given an integer, convert it to a roman numeral.
//Input is guaranteed to be within the range from 1 to 3999.
//char* intToRoman(int num) {
//    char *table_qian[4]={"","M","MM","MMM"};
//    char *table_bai[10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
//    char *table_shi[10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
//    char *table_ge[10]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
//    int qian,bai,shi,ge;
//    qian = num/1000;
//    bai = (num-1000*qian)/100;
//    shi = (num%100)/10;
//    ge = num%10;
//    char *b = (char *)malloc(16);
//    b = strcat(strcat(strcat(strcat(b,table_qian[qian]),table_bai[bai]),table_shi[shi]),table_ge[ge]);
//    //printf("%s",b);
//    return b;
//}

//11.装最多的水(O(n)算法),Container With Most Water
//Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). 
//n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). 
//Find two lines, which together with x-axis forms a container, such that the container contains the most water.
//int maxArea(int* height, int heightSize) {
//    int Area_max=0,i=0,j=heightSize-1,h=0;
//    while (i<j)
//    {
//        h = min(*(height+i),*(height+j));//一开始选择最宽的两条线,然后再往中间尝试,只有当线高度比h更高,才去尝试计算面积并比较最大值
//        Area_max = max(Area_max,(j-i)*h);
//        if (*(height+i)<=h)
//        {
//            i++;
//        }
//        if (*(height+j)<=h)
//        {
//            j--;
//        }
//    }
//    return Area_max;
//}

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值