leetcode

3. Longest Substring Without Repeating Characters

给定一个字符串,找到最长子串的长度,而不重复字符。 

https://www.cnblogs.com/K-artorias/p/7665604.html

5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
int expandAroundCenter(char *s, int left, int right){
    int len=0;
    char *p;
    p=s;
    while(*p){
        p++;
        len++;
    }
    int L=left;
    int R=right;
    while(L>=0 && R< len && *(s+L)==*(s+R)){
        L--;
        R++;
    }
    return R-L-1;
}

char* longestPalindrome(char* s) {

    char *p;
    p=s;
    int len=0;
    while(*p){
        len++;
        p++;
    }
    
    int start=0; 
    int end=0;
    for (int i=0; i<len; i++){
        int len1=expandAroundCenter(s, i, i);
        int len2=expandAroundCenter(s, i, i+1);
        int len=len1>len2?len1:len2;
        
        if(len>end-start){
            start=i-(len-1)/2;
            end=i+len/2;
        }
        
    }
    
    char *resP;
    char res[2000] = {0};
    resP=res;
    p=s;

    int j=0;
    for(int i=start;i<=end;i++){
        res[j]=*(p+i);
        j++;
    }
    res[j]=''; 
        
    return resP   ; 
    
}

21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    
    struct ListNode* p1;
    struct ListNode* p2;
    
    struct ListNode* start;
    struct ListNode* end;
    
    start=end=NULL;
    
    p1=l1;
    p2=l2;

    if (l1 == NULL && l2 == NULL){
        return NULL;
    }
    
    if (l1 == NULL && l2){
        return l2;
    }else if(l1 && l2 == NULL){
        return l1;
    }
    
    if (p1->val < p2->val){
        start =p1;
        p1=p1->next;
    }else{
        start =p2;
        p2=p2->next;      
    }
    end=start;
    
    while(p1 && p2){
        if(p1->val > p2->val){
            end->next=p2;
            end=p2;
            p2=p2->next;
        }else{
            end->next=p1;
            end=p1;
            p1=p1->next;
        }
    }
    
    if(!p1){
        end->next=p2;
    }
    
    if(!p2){
        end->next=p1;
    }
    
    return start;
}


  

原文地址:https://www.cnblogs.com/lxs0731/p/9348515.html