头像

cwr




离线:3小时前


最近来访(101)
用户头像
harper886
用户头像
绝世天才大能猫
用户头像
fspkj6
用户头像
叶落孤城
用户头像
未雨绸缪
用户头像
check_check
用户头像
培风
用户头像
不会DP不改名de
用户头像
huangbq
用户头像
iiiiiiire
用户头像
溯光
用户头像
zyyyds
用户头像
量子态发圈
用户头像
瀛_1
用户头像
honndaj
用户头像
Sundae
用户头像
哈士奇起先
用户头像
加油卷死对手
用户头像
Luli
用户头像
不拿国一不改名


cwr
1个月前
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define quick ios::sync_with_stdio(false),cin.tie(0); 
vector <vector<int>> result;
vector<int> path;

void dfs(int n,int k,int startIndex) {
     if(path.size()==k) {
        for(auto it=path.begin();it!=path.end();it++) {
            cout<<*it<<' ';
        }
        cout<<"\n";
        return;
     }
     for(int i=startIndex;i<=n;i++) {
        path.push_back(i);
        dfs(n,k,i+1);
        path.pop_back();
     }
}
void solved() {
    int n,k;
    cin>>n>>k;  
    dfs(n,k,1);
}
signed main() {
    quick;
    solved();
    return 0;
}



cwr
1个月前
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *p = headA, *q = headB;
        while (p != q)
        {
            if (p) p = p->next;
            else p = headB;
            if (q) q = q->next;
            else q = headA;
        }
        return p;
    }
};




cwr
1个月前
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if (m == n) return head;
        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode *p = dummy;
        for (int i = 0; i < m - 1; i ++ )
            p = p->next;
        ListNode *a = p, *b = a->next, *c = b->next;
        for (int i = m + 1; i <= n; i ++ )
        {
            ListNode *d = c->next;
            c->next = b;
            b = c;
            c = d;
        }
        a->next->next = c;
        a->next = b;
        return dummy->next;
    }
};





cwr
1个月前
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *prev = nullptr;
        ListNode *cur = head;
        while (cur)
        {
            ListNode *next = cur->next;
            cur->next = prev;
            prev = cur, cur = next;
        }
        return prev;
    }
};




cwr
1个月前
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        auto dummy = new ListNode(-1);
        dummy->next = head;

        for (auto p = dummy; p && p->next && p->next->next; p = p->next->next)
        {
            auto a = p->next, b = a->next;
            p->next = b;
            a->next = b->next;
            b->next = a;
        }

        return dummy->next;
    }
};



活动打卡代码 LeetCode 61. Rotate List

cwr
1个月前
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if (!head) return head;

        int n = 0;
        ListNode *p = head;
        while (p)
        {
            n ++ ;
            p = p->next;
        }

        k %= n;
        if (!k) return head;


        ListNode *first = head;
        while (k -- && first) first = first->next;
        ListNode *second = head;
        while (first->next)
        {
            first = first->next;
            second = second->next;
        }
        first->next = head;
        head = second->next;
        second->next = 0;
        return head;
    }
};




cwr
1个月前
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head) return head;
        ListNode* p = head;
        while (p->next)
        {
            if (p->val == p->next->val) p->next = p->next->next;
            else p = p->next;
        }
        return head;
    }
};




cwr
1个月前
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};



cwr
1个月前
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        auto first = dummy, second = dummy;
        while (n -- ) first = first->next;
        while (first->next) 
        {
            first = first->next;
            second = second->next;
        }

        second->next = second->next->next;

        return dummy->next;
    }
};





cwr
1个月前
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string res;
        dfs1(root, res);
        return res;
    }

    void dfs1(TreeNode* root, string& res)
    {
        if (!root)
        {
            res += "#,";
            return;
        }
        res += to_string(root->val) + ',';
        dfs1(root->left, res);
        dfs1(root->right, res);
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        int u = 0;
        return dfs2(data, u);
    }

    TreeNode* dfs2(string &data, int &u)
    {
        if (data[u] == '#')
        {
            u += 2;
            return NULL;
        }

        int t = 0;
        bool is_minus = false;
        while (data[u] != ',')
        {
            if (data[u] == '-') is_minus = true;
            else t = t * 10 + data[u] - '0';
            u ++ ;
        }
        u ++ ;
        if (is_minus) t = -t;

        auto root = new TreeNode(t);
        root->left = dfs2(data, u);
        root->right = dfs2(data, u);

        return root;
    }
};