头像

要进亚研NLP和狗家AI组的男人




离线:2天前



class Solution {
public:
    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        auto p = list1, q = list1;
        for (int i = 0; i < a - 1; i ++ ) p = p->next;
        for (int i = 0; i < b + 1; i ++ ) q = q->next;
        p->next = list2;
        while (list2->next) list2 = list2->next;
        list2->next = q;
        return list1;   
    }
};



class Solution {
public:
    vector<int> printListReversingly(ListNode* head) {
    vector<int>ret;
    while(head)
    {
        ret.push_back(head->val);
        head=head->next;
    }
    reverse(ret.begin(),ret.end());
    return ret;
    }
};



        int k = 0;
        for (int i = 0; i < nums.size(); i ++ )
            if (!i || nums[i] != nums[i - 1])
                nums[k ++ ] = nums[i];
        return k;



class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
    auto p1 = head;
    while(p1)
    {
        if(p1->next && p1->val == p1->next->val)
                 p1->next = p1->next->next;
        else
           p1 = p1->next;
    }
    return head;
    }
};



class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    if(l1==nullptr)return l2;
    if(l2==nullptr)return l1;
    if(l1->val<=l2->val)
    {
            l1->next = mergeTwoLists(l1->next,l2);
            return l1;
    }
    else 
    {
            l2->next = mergeTwoLists(l1,l2->next);
            return l2; 
    }
    }
};



class Solution {
public:
    int romanToInt(string s) {
        int tagVal[128];
        tagVal['I'] = 1;
        tagVal['V'] = 5;
        tagVal['X'] = 10;
        tagVal['L'] = 50;
        tagVal['C'] = 100;
        tagVal['D'] = 500;
        tagVal['M'] = 1000;
        int val = 0;
        for(int i =0;i<s.length();i++){
            if(i+1 >= s.length() || tagVal[s[i+1]] <= tagVal[s[i]])
                val += tagVal[s[i]];
            else
            {
                /* 前一个字符比后一个字符小iv 特例 */
                val -= tagVal[s[i]];
            }

        }
        return val;
    }
};



class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
    vector<int>ret;
    unordered_map<int,int>p;
    for(int i= 0;i<nums.size();i++)
    {
        int we = target - nums[i];
        if(p.count(we))
        {
            ret = vector<int>{we,nums[i]};
            break;
        }
        p[nums[i]]=i;
    }
    return ret;
    }
};


活动打卡代码 AcWing 789. 数的范围

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int q[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);

    while (m -- )
    {
        int x;
        scanf("%d", &x);

        int l = 0, r = n - 1;
        while (l < r)
        {
            int mid = l + r >> 1;
            if (q[mid] >= x) r = mid;
            else l = mid + 1;
        }

        if (q[l] != x) cout << "-1 -1" << endl;
        else
        {
            cout << l << ' ';

            int l = 0, r = n - 1;
            while (l < r)
            {
                int mid = l + r + 1 >> 1;
                if (q[mid] <= x) l = mid;
                else r = mid - 1;
            }

            cout << l << endl;
        }
    }

    return 0;
}



class Solution {
public:
    void deleteNode(ListNode* node) {
    node->val=node->next->val;
    node->next=node->next->next;
    }
};


活动打卡代码 LeetCode 20. 有效的括号

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;

        for (auto c : s) {
            if (c == '(' || c == '[' || c == '{') stk.push(c);
            else {
                if (stk.size() && abs(stk.top() - c) <= 2) stk.pop();
                else return false;
            }
        }

        return stk.empty();
    }
};