头像

zzl_9


访客:357

离线:3天前



zzl_9
26天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    // vector<int> ans;
    // void reverse(ListNode* head){
    //     if (head->next == nullptr) {
    //         ans.push_back(head->val);
    //     }
    //   reverse(head->next);
    //   ans.push_back(head->val);

    // }
    vector<int> printListReversingly(ListNode* head) {
        vector<int> res;
        while(head){
            res.push_back(head->val);
            head = head->next;
        }
       return vector<int>(res.rbegin(), res.rend());
    }

};



zzl_9
26天前
class Solution {
public:
    void swap (vector<int> &arr, int x, int y){
        int t = arr[x];
        arr[x] = arr[y];
        arr[y] = t;
    }
    void reOrderArray(vector<int> &array) {
        if (array.size() == 0) return;
         int l = 1, r = array.size() - 1;
         int p = array[0];
         while(l <= r){
             if (array[l] % 2 == 1) l++;
             else{
                 swap(array,l,r);
                 r--;
             }
         }
         swap(array,0,r);

    }
};



zzl_9
26天前
class Solution {
public:
    int getMissingNumber(vector<int>& nums) {
        set<int> s;
        for (int cur: nums) s.insert(cur);
        int i = 0;
        for (; i < nums.size(); i++){
            if (s.find(i) == s.end()){
                break;
            }
        }
        return i;
    }
};



zzl_9
26天前
class Solution {
public:
    int getNumberOfK(vector<int>& nums , int k) {
        multiset<int> s;
        for (int i = 0; i < nums.size(); i++)
        s.insert(nums[i]);
        return s.count(k);
    }
};



zzl_9
27天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplication(ListNode* head) {
        auto dummy = new ListNode(-1);
        dummy -> next = head;
        auto p = dummy;
        while(p -> next){
            auto q = p -> next;
            while(q && p -> next -> val == q -> val) q = q-> next;
            if (p -> next -> next == q ) p = p -> next;
            else p -> next = q;
        }
        return dummy -> next;
    }
};



zzl_9
27天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
        auto l1 = headA, l2 = headB;
        while(l1 != l2){
            l1 = l1 == nullptr ? headB : l1 -> next;
            l2 = l2 == nullptr ? headA : l2 -> next;
        }
        return l1;
    }
};


活动打卡代码 AcWing 35. 反转链表

zzl_9
28天前
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* cur = head;
        while(cur){
            ListNode* last = cur -> next;
            cur -> next = prev;
            prev = cur;
            cur = last;
        }
        return prev;
    }
};



zzl_9
28天前
class Solution {
public:
    int strToInt(string str) {
        int k = 0;
        int len = str.size();
        while ( k < len && str[k] == ' ') k++;
        int minus = 1;
        long long res = 0;
        if ( k < len){
            if (str[k] == '-') minus = -1, k++;
        else if (str[k] == '+')k++;
        }
        while (k < len && str[k] >= '0' && str[k] <= '9')
        {
            res = res * 10 + str[k] - '0';
            if (res > 1e11) break;
            k ++ ;
        }

        res *= minus;
        if (res > INT_MAX) res = INT_MAX;
        if (res < INT_MIN) res = INT_MIN;

        return res;


    }
};


活动打卡代码 AcWing 78. 左旋转字符串

zzl_9
28天前
class Solution {
public:
    string leftRotateString(string str, int n) {
        string f,e;
        for (int i = 0; i < n; i++) e += str[i];
        for (int i = n; i < str.size(); i++) f += str[i];
        return f + e;
    }
};



zzl_9
28天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {

public:
    ListNode* merge(ListNode* l1, ListNode* l2) {
        auto nn = new ListNode(0);

        auto l = l1,r = l2,cur = nn;
        while (l && r){
            if (l -> val < r -> val) {
                auto x = new ListNode(l -> val);
                cur -> next = x;
                cur = x;
                l = l -> next;
            }else{
                auto x = new ListNode(r -> val);
                cur -> next = x;
                cur = x;
                r = r -> next;
            }
        }
        if (l){
            while(l){
                auto x = new ListNode(l -> val);
                cur -> next = x;
                cur = x;
                l = l -> next;
            }
        }else if(r){
            while(r){
                auto x = new ListNode(r -> val);
                cur -> next = x;
                cur = x;
                r = r -> next;
            }
        }
        return nn -> next;
    }  
};