1. 程式人生 > >leetcode 中等題

leetcode 中等題

2. Add Two Numbers(中等)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* first = l1;
        ListNode
* second = l2; int result1=0,result2=0; long long result=0; stack<int> S; int count=0; while(l1 != NULL){ S.push(l1->val); l1 = l1->next; } count = S.size()-1; while(!S.empty()){ result1
+= pow(10,count--) * S.top(); S.pop(); } while(l2 != NULL){ S.push(l2->val); l2 = l2->next; } count = S.size()-1; while(!S.empty()){ result2 += pow(10,count--) * S.top(); S.pop(); } result
= result1 + result2; delete first,second; // ListNode*p = new ListNode(-1); //頭節點 ListNode* pHead = NULL; ListNode* p = pHead; if(!result){ ListNode* q = new ListNode(0); pHead = q; } while(result){ int val = result % 10; result /= 10; ListNode* q = new ListNode(val); if(pHead == NULL){ pHead = q; p = q; } else{ p->next = q; p = p->next; } } return pHead; } };
此方法會溢位,超過long long 型範圍
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {   //正確解法,按位相加,注意進位
    ListNode *head = NULL, *prev = NULL;
    int carry = 0;
    while (l1 || l2) {
        int v1 = l1? l1->val: 0;
        int v2 = l2? l2->val: 0;
        int tmp = v1 + v2 + carry;
        carry = tmp / 10;
        int val = tmp % 10;
        ListNode* cur = new ListNode(val);
        if (!head) head = cur;
        if (prev) prev->next = cur;
        prev = cur;
        l1 = l1? l1->next: NULL;
        l2 = l2? l2->next: NULL;
    }
    if (carry > 0) {
        ListNode* l = new ListNode(carry);
        prev->next = l;
    }
    return head;
}

 3, Longest Substring Without Repeating Characters.(很慢)

int lengthOfLongestSubstring(string s) {

    int maxLength = 0;
    int count;
    for(int i=0;i<s.length();i++){
        string currString = "";
        count = 0;
        for(int j=i;j<s.length();j++){
            if(currString.find(s[j])>=currString.length()){
                currString += s[j];
                count++;
                cout<<s[j]<<endl;
                cout<<"string:"<<currString<<endl; 
                if(count>maxLength) maxLength = count;
            } 
            else{
                break;
            }
        }
    }
    return maxLength;
}

5, Longest Palindromic Substring.(很慢)

string longestPalindrome(string s) {
    string maxString = "";
    int maxLength = 0;
    int count = 0;
        
    for(int i=0;i<s.length();i++){
        string Str = "";
        int index = s.rfind(s[i]);
        int start = i;
        int end = index;
        int count = 0;
        int ii=start;
            
        while(ii<=index){
            if(s[ii]==s[index]){ii++;index--;}
            else{
                ii = start;
                index = end-1;
                end--;
            }   //匹配失敗
        }
            
        if(ii>index){  //匹配成功
            for(int k=start;k<=end;k++)
                Str += s[k];
            count = Str.length();
        }
        if(count>=maxLength) {maxString = Str;maxLength=count;}
    }
    return maxString;
}