LeetCode 1~10题C语言题解以及相应说明

比较早写的,有些题目可能需要进行适配

//10.正则表达式:Implement regular expression matching with support for '.' and '*'.
//'.' Matches any single character.
//'*' Matches zero or more of the preceding element.
//The matching should cover the entire input string (not partial).
//动态规划思想,即当前匹配的结果是建立在上一步匹配结果的基础上,与递归有点相似,却使用数组去存放匹配结果。
//bool isMatch(char* s, char* p) {
//    int len_s = strlen(s),len_p = strlen(p),count=0;
//    bool **dp = (bool **)malloc(sizeof(bool *)*(len_s+1));//len_s+1行
//    for (int i = 0; i < len_s+1; i++)
//    {
//        dp[i] = (bool *)malloc(sizeof(bool)*(len_p+1));     //每行len_p+1列
//        memset(dp[i],0,sizeof(dp[i]));
//    }
//    dp[0][0] = true;//d[i][j]表示串s中前i个与p中前j个匹配的结果,其中认为dp[0][0]是true
//    for (int i = 0; i <= len_s; i++)
//    {
//        for (int j = 1; j <= len_p; j++)
//        {
//            if (*(p+j-1)!='*')
//            {
//                dp[i][j] = i>0 && dp[i-1][j-1] && (*(s+i-1)==*(p+j-1)||*(p+j-1)=='.');//如果*(p+j-1)!='*'并且*(s+i-1)==*(p+j-1)||*(p+j-1)=='.',那么dp[i][j]=dp[i-1][j-1]
//            }else
//            {
//                if (i==0 || (*(s+i-1)!=*(p+j-2) && *(p+j-2)!='.'))//如果*(p+j-1)=='*',匹配dp[i][j] = dp[i][j-2]的两种情况,一个是i==0,另外一个类似于aab匹配aabc*
//                {
//                    dp[i][j] = dp[i][j-2];
//                }else
//                {
//                    dp[i][j] = dp[i-1][j] || dp[i][j-1] || (j>1 && dp[i][j-2]);//dp[i][j-1]表示*号之前的符号用不止一次,dp[i][j-2]表示不用*号之前的符号,dp[i-1][j]表示串s中回退一个符号进行匹配
//                }
//                
//            }
//        }
//    }
//    return dp[len_s][len_p];
//}

//9.数字是否为回文形式:比如1221,23123等。Determine whether an integer is a palindrome. Do this without extra space.
//bool isPalindrome(int x) {
//    int rev=0;
//    if (x<0 || (x%10==0 && x!=0))
//    {
//        return false;
//    }
//    while (x>rev)
//    {
//        rev = rev*10 + x%10;                //算法核心,即倒过来
//        x=x/10;
//        if (x<10)
//        {
//            break;
//        }
//    }
//    return (x==rev || x==rev/10);
//}

//8.Implement atoi to convert a string to an integer.即实现字符串转换成数字
//需要考虑各种不同的情况,比如空格,字母,正负号,溢出等情况
//int myAtoi(char* str) {
//    int i=0,count_p=0,count_n=0,index_other=0,index_num=0;  //count_p正号个数,count_n负号个数,index_other非数字指标,index_num数字指标
//    double result=0;                                       //结果,由于需要考虑溢出,因此需要64位存放
//    while (*(str+i)!=0)
//    {
//        if(!index_other){
//            if (*(str+i)>=48 && *(str+i)<=57)          //数字对应的ascii码
//            {
//              result = result*10+*(str+i)-48;
//                index_num = 1;
//            }else if(*(str+i)==45){                    //负号
//              count_n++;
//                index_num = 1;
//                if (*(str+i+1)<48 || *(str+i+1)>57)
//                {
//                    return 0;
//                }
//            }else if(*(str+i)==43){                   //正号
//              count_p++;
//                index_num = 1;
//                if (*(str+i+1)<48 || *(str+i+1)>57)
//                {
//                    return 0;
//                }
//            }else if(*(str+i)==32)          //空格处理
//            {
//                if (index_num)
//                {
//                    break;
//                }else 
//                {
//                    continue;              //空格前如果没有数字,则继续,作用为忽略多个空格
//                }
//            }else
//            {
//                index_other=1;
//            }
//        }else{                         //非正负号,非数字,非空格的处理
//            break;
//        }
//        i++;
//    }
//    if(count_n>count_p){
//        result = -result;
//    }else if(result < double(-2147483648)){    //处理溢出情况,vs好像不能运行,但是leetcode上可以
//        result = -2147483648;
//    }else if(result > double(2147483647)){   //处理溢出情况,vs好像不能运行,但是leetcode上可以
//        result = 2147483647;
//    }
//    return (int)result;
//}

//7.反向输出数字Reverse digits of an integer,Your function should return 0 when the reversed integer overflows
//Example1: x = 123, return 321
//Example2: x = -123, return -321
//int reverse(int x) {
//    int rev=0,tmp=0,rev_tmp=0;
//    while (x!=0)
//    {
//        tmp = x%10;
//        rev_tmp = rev*10 +tmp;                //算法核心,即倒过来
//        if ((rev_tmp-tmp)/10!=rev)           //计算溢出,即不满足上述等式
//        {
//            return 0;
//        }
//        rev = rev_tmp;
//        x=x/10;
//    }
//    return rev;
//}

//6. 字母之字型排列,然后再输出,如下
//The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows
//P   A   H   N
//A P L S I I G
//Y   I   R
//And then read line by line: "PAHNAPLSIIGYIR"
//char* convert(char* s, int numRows) {
//    int len = strlen(s);
//    int col = len+1;    //取最大情况再加一列
//    char *s_result = (char *)malloc(sizeof(char)*len+1);
//    char **tmp = (char **)malloc(sizeof(char *)*numRows);//numRows行,tmp矩阵是用于存放排列成之字形整个图形,无字母处用'\0'表示;
//    for (int i = 0; i < numRows; i++)
//    {
//        tmp[i] = (char *)malloc(sizeof(char)*col);     //col列
//
//    }
//    for (int i = 0; i < col; i++)
//    {
//        for (int j = 0; j < numRows; j++)
//        {
//            tmp[j][i] = 0;
//        }
//    }
//    int count=0,i_row=0,j_col=0;
//    int len_tmp = len-1,numRows_tmp = numRows-1;
//    if (!numRows_tmp)
//    {
//        return s;
//    }else
//    {
//        while (count<=len_tmp)
//        {
//            if (j_col%numRows_tmp==0)   //类似第一列,需要排满所有字母
//            {
//                for (int i = 0; i <numRows && count<len; i++)
//                {
//                    tmp[i][j_col] = s[count];
//                    count++;
//                
//                }
//                j_col++;
//            }else                     //其他列,每列仅有一个字母,行列号满足[numRows_tmp-j_col%numRows_tmp][j_col]
//            {
//                if (count<len)
//                {
//                    tmp[numRows_tmp-j_col%numRows_tmp][j_col] = s[count];
//                    count++;
//                    j_col++;
//                }
//            
//            }
//        }
//        int k = 0;
//        for (int i = 0; i < numRows; i++)    //将二维数组转换成一维并输出,其中为'\0'忽略
//        {
//            for (int j = 0; j < col; j++)
//            {
//        //        printf("%c",tmp[i][j]);
//                if (tmp[i][j]!='\0')
//                {
//                    s_result[k] = tmp[i][j];
//                    k++;
//                }
//            }
//        //    printf("\n");
//        }
//        s_result[k] = '\0';
//        return s_result;
//    }
//}

//5.即求一段字符串里面的最大回文子串
//Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
//Input: "babad"
//Output: "bab"
//Note: "aba" is also a valid answer.
//Input: "cbbd"
//Output: "bb"
//char* longestPalindrome(char* s) {
//    int Length1 = strlen(s)+1;
//    int index;                        //用于标识最大回文子串的起始位置
//    int Length = Length1-1;
//    int count=0;                      //最大回文子串的长度
//    if (Length==1)                    //如果字符串只有一个字符,则最大回文子串即该字符串本身
//    {
//        return s;
//    }else
//    {
//        for (int i = 0; i < Length; i++)
//        {
//            int count_before1=0,count_before2=0;      //用于存放本次循环中,bab模式和bb模式的回文子串长度
//            if(i>0 && s[i-1]==s[i+1]){                //统计bab模式下,最大回文子串长度
//                count_before1=count_before1+3;
//                for (int j = 1; j < Length-1-i; j++)  //将bab模式进行扩展(如cbabc),统计该次循环中最大回文子串长度
//                {
//                    if ((i-j)>=1 && s[i-j-1]==s[i+j+1])
//                    {
//                        count_before1=count_before1+2;
//                    }else
//                    {
//                        break;
//                    }
//                }
//                if (count<count_before1)             //如果该次长度大于原先最大回文子串长度,则记录长度与位置
//                {
//                    count = count_before1;
//                    index = i-count_before1/2;       //index表示回文长度的起始位置,是以位置i为对称的
//                }
//            } 
//            if (s[i]==s[i+1])                         //统计bb模式下,最大回文子串长度
//            {
//                if(!i){                               //统计从第一个字符开始的bb模式最大回文子路,!i即表示i==0
//                    count_before2=count_before2+2;
//                    for (int j = 2; j < Length; j++)  //将bb模式进行扩展(如bbbb),统计该次循环中最大回文子串长度
//                    {
//                        if (s[0]==s[j])
//                        {
//                            count_before2++;
//                        }else
//                        {
//                            break;
//                        }
//                    }
//                    if (count<count_before2)
//                    {
//                        count = count_before2;
//                        index = 0;
//                    }
//                }else
//                {
//                    count_before2=count_before2+2;      
//                    for (int j = 1; j < Length-1-i; j++)   //统计cbbc模式最大回文子路,即s[i-j]==s[i+j+1]
//                    {
//                        if (i>=j && s[i-j]==s[i+j+1])
//                        {
//                            count_before2=count_before2+2;
//                        }else
//                        {
//                            break;
//                        }
//                    }
//                    if (count<count_before2)
//                    {
//                        count = count_before2;
//                        index = i-count_before2/2+1;
//                    }
//                }
//            }
//        
//        }
//        
//        if (count==0)                                            //count==0表示整个字符串均没有满足bb模式或者bab模式的回文,则认为最大回文长度为1,即单个字符,返回值第一个字符
//        {
//            char *store = (char *)malloc(sizeof(char)*(count+2));
//            store[0]=s[0];
//            store[1]='\0';
//            return store;
//        }else
//        {
//            char *store = (char *)malloc(sizeof(char)*(count+1)); //count大于0表示存在长度大于1的回文,则根据index与count进行截取
//            for (int i = 0; i < count; i++)
//            {
//                store[i]=s[index+i];
//            }
//            store[count]='\0';                                    //c语言的字符串最终以'\0'结尾
//            return store;
//        }
//        
//    }
//}

//4.There are two sorted arrays nums1 and nums2 of size m and n respectively.
//Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
//nums1 = [1, 2]
//nums2 = [3, 4]
//The median is (2 + 3)/2 = 2.5
//nums1 = [1, 3]
//nums2 = [2]
//The median is 2.0
//解题思路如下所示:
    //      left_part          |        right_part
    //A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]
    //B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]
//关键是满足B[j-1]<=A[i]以及A[i-1]<=B[j],并且左右两边元素个数一样
//double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
//    int i,j,i_min,i_max;
//    if (nums1Size>nums2Size)  //交换数组,需要保证nums1长度比较小
//    {
//        int *temp=nums1;
//        nums1 = nums2;
//        nums2 = temp;
//        int tmp = nums1Size;
//        nums1Size = nums2Size;
//        nums2Size = tmp;
//    }
//    i_min=0;
//    i_max=nums1Size;
//    while (i_min<=i_max)  //以下进行二分法查找
//    {
//        i = (i_min+i_max)/2;
//        j = (nums1Size+nums2Size+1)/2-i;
//        if(i<nums1Size && nums2[j-1]>nums1[i])  //需要找到满足nums2[j-1]<=nums1[i] nums1[i-1]<=nums2[j],才行
//        {
//            i_min = i+1;
//        }else if(i>0 && nums1[i-1]>nums2[j])  //需要找到满足nums2[j-1]<=nums1[i] nums1[i-1]<=nums2[j],才行
//        {
//            i_max = i-1;
//        }else                                //以下是满足nums2[j-1]<=nums1[i] nums1[i-1]<=nums2[j]之后的处理过程以及一些边界值的处理
//        {
//            int max_left=0,min_right=0;
//            if (i==0)
//                {max_left = nums2[j-1];}     //i为0,表示nums1只有一个元素,并且是最大的,nums2的前(nums1Size+nums2Size+1)/2-i个元素放左边
//            else if (j==0)                
//                {max_left = nums1[i-1];}     //j为0,表示nums2只有一个元素,并且是最大的,nums1的前(nums1Size+nums2Size+1)/2-i个元素放左边
//            else if(nums2[j-1]>nums1[i-1])  //i,j不为0,则左边最大的元素是max(nums2[j-1],nums1[i-1])
//                {max_left = nums2[j-1];}
//            else
//                {max_left = nums1[i-1];}
//            if ((nums1Size+nums2Size)%2)    //如果总的元素个数为奇数,那么中间数即max_left
//            {
//                return max_left;
//            }
//            if (i==nums1Size)              //i为nums1Size,表示所有的nums1都在左边,因此右边最小的元素为nums2[j]
//                {min_right = nums2[j];}
//            else if (j==nums2Size)         //j为nums2Size,表示所有的nums2都在左边,因此右边最小的元素为nums1[i]
//                {min_right = nums1[i];}
//            else if(nums2[j]<nums1[i])     //i,j不为各自的最大值,则右边最大的元素是min(nums2[j],nums1[i])
//                {min_right = nums2[j];}
//            else
//                {min_right = nums1[i];}
//            return (max_left+min_right)/2.0;
//        }
//        
//    }
//        
//}

//3.Given a string, find the length of the longest substring without repeating characters.
//Given "abcabcbb", the answer is 3.
//Given "", the answer is 0.
//Given "bbbbb", the answer is 1.
//Given "dvdf", the answer is 3.
//Given "pwwkew", the answer is 3
//int lengthOfLongestSubstring(char* s) {
//    int count=0,max,jump=0;
//    while (*(s+count)!='\0')
//    {
//        count++;
//    }
//    int *tmp = (int*)malloc(sizeof(int)*count);
//    if (count==0)                          //需要考虑空字符的情况
//    {
//        *tmp = max = 0;
//    }else
//    {
//        *tmp = max = 1;
//    }
//    
//    for (int i = 1; i < count; i++)   //取出第i个,然后从0到i-1个与第i个进行比较,看有没有相同的
//    {
//        *(tmp+i)=1;
//        for (int k = jump; k < i; k++)
//        {
//            if (*(s+k)!=*(s+i))       //如果不相同,那么继续进行比较,并且值加1
//            {
//                *(tmp+i) = *(tmp+i)+1;
//            }else
//            {
//                *(tmp+i) = 1;         //如果有发生重复,则计数变为1
//                jump = k+1;           //如果有发生重复,则从第一个重复之后的那个字母开始接着比
//            }
//        }
//        if (*(tmp+i)>max)
//        {
//            max = *(tmp+i);
//        }
//    }
//    return max;
//}

//2.You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
//Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
//Output: 7 -> 0 -> 8
//struct ListNode {
//    int val;
//    struct ListNode *next;
//};
//struct ListNode *creat(){                  //产生链表,以-99为标识表示结尾
//    struct ListNode *q,*p,*head;
//    q = (struct ListNode *)malloc(sizeof(struct ListNode));
//    head = q;
//    p = q;
//    scanf("%d",&q->val);
//    while (q->val!=-99)
//    {
//        q = (struct ListNode *)malloc(sizeof(struct ListNode));
//        p->next = q;
//        p = q;
//        scanf("%d",&q->val);
//    }
//    q->next = NULL;
//    return head;
//}
//struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
//    struct ListNode *resultp,*resultq,*result_head;
//    result_head = resultp = resultq = (struct ListNode *)malloc(sizeof(struct ListNode));
//    int carry=0;
//    while (l1->next!=NULL && l2->next!=NULL)         //如果都有数据,则进行相加运算,考虑进位
//    {
//        resultq->val = l1->val+l2->val+carry;
//        carry = resultq->val/10;
//        resultq->val = resultq->val%10;
//        resultp = resultq;
//        resultq = (struct ListNode *)malloc(sizeof(struct ListNode));
//        resultp->next = resultq;
//        l1 = l1->next;
//        l2 = l2->next;
//    }
//    if (l1->next==NULL && l2->next==NULL)    //如果都无数据,则返回,考虑进位
//    {
//        if(carry){
//        resultq->val = carry;
//        resultq->next = NULL;
//        resultp->next = resultq;
//        }else
//        {
//            resultq = NULL;
//            resultp->next = resultq;
//        }
//        return result_head;
//    }else if(l1->next==NULL)     //如果l1无数据,则继续进行相加运算(仅l2与进位)
//    {
//        while (l2->next!=NULL)
//        {
//            resultq->val = l2->val+carry;
//            carry = resultq->val/10;
//            resultq->val = resultq->val%10;
//            resultp = resultq;
//            resultq = (struct ListNode *)malloc(sizeof(struct ListNode));
//            resultp->next = resultq;
//            l2 = l2->next;
//        }
//    }else if(l2->next==NULL){        //如果l2无数据,则继续进行相加运算(仅l1与进位)
//        while (l1->next!=NULL)
//        {
//            resultq->val = l1->val+carry;
//            carry = resultq->val/10;
//            resultq->val = resultq->val%10;
//            resultp = resultq;
//            resultq = (struct ListNode *)malloc(sizeof(struct ListNode));
//            resultp->next = resultq;
//            l1 = l1->next;
//        }
//    }
//    if(carry){                           //加完之后如果还有进位,则需要添加一位,由于前面while循环已经有新生成节点了,所以此处不需要再生成
//        resultq->val = carry;
//        resultq->next = NULL;
//        resultp->next = resultq;
//    }else                                 //加完之后如果没有进位,则无需添加一位,由于前面while循环已经有新生成节点了,所以此处需要将节点地址置0
//    {
//        resultq = NULL;
//        resultp->next = resultq;
//    }
//    return result_head;
//}

//1.twoSum:Given an array of integers, return indices of the two numbers such that they add up to a specific target.
//Given nums = [2, 7, 11, 15], target = 9,
//Because nums[0] + nums[1] = 2 + 7 = 9,
//return [0, 1].
//int* twoSum(int* nums, int numsSize, int target) {
//    int *p;
//    p = (int*)malloc(sizeof(int) * numsSize);
//    for(int i=0;i<numsSize;i++){
//        for(int j=i+1;j<numsSize;j++){
//            if(nums[i]+nums[j]==target){
//                p[0] = i;
//                p[1] = j;
//                return p;
//            }
//        }
//    }
//  p[0] = -1;                  //如果没有满足条件的,返回-1
//  p[1] = -1;
//  return p;
//}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值