头像

optimjie

山东科技大学


访客:15705

离线:22小时前



optimjie
11天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> ans;
    vector<int> inorderTraversal(TreeNode* root) {
        dfs(root);
        return ans;
    }
    void dfs(TreeNode *root) {
        if (!root) return;
        dfs(root->left);
        ans.push_back(root->val);
        dfs(root->right);
    }
};


活动打卡代码 LeetCode 93. 复原IP地址

optimjie
11天前
class Solution {
public:
    vector<string> ans;
    string w;
    int n;
    vector<string> restoreIpAddresses(string s) {
        w = s;
        n = s.size();
        dfs(0, 0, "");
        return ans;
    }
    void dfs(int u, int s, string str) {
        if (s < 4 && u >= n) return;
        if (s == 4) {
            if (u == n) {
                int m = str.size();
                ans.push_back(str.substr(0, m - 1));
            }
            return;
        }
        for (int i = 0; i < 3 && u + i < n; i++) {
            string t = w.substr(u, i + 1) + ".";
            if (i != 0 && t[0] == '0') continue;
            if (i < 2) {
                dfs(u + i + 1, s + 1, str + t);
            } else {
                int x = (w[u] - '0') * 100 + (w[u + 1] - '0') * 10 + (w[u + 2] - '0');
                if (x <= 255) {
                    dfs(u + i + 1, s + 1, str + t);
                }
            }
        }
    }
};


活动打卡代码 LeetCode 92. 反转链表 II

optimjie
12天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        auto dummy = new ListNode(-1);
        dummy->next = head;
        auto a = dummy;
        for (int i = 0; i < m - 1; i++) a = a->next;
        auto b = a->next, c = b->next;
        for (int i = 0; i < n - m; i++) {
            auto ne = c->next;
            c->next = b;
            b = c;
            c = ne;
        } 
        a->next->next = c;
        a->next = b;
        return dummy->next;
    }
};


活动打卡代码 LeetCode 91. 解码方法

optimjie
12天前
class Solution {
public:
    int numDecodings(string s) {
        int n = s.size();
        if (n == 0) return 0;
        vector<int> f(n);
        if (s[0] != '0') f[0] = 1;
        for (int i = 1; i < n; i++) {
            if (s[i] >= '1' && s[i] <= '9') f[i] = f[i - 1];
            if (i >= 1) {
                int x = (s[i - 1] - '0') * 10 + (s[i] - '0');
                if (i == 1) {
                    if (x >= 10 && x <= 26) f[i]++;
                } else {
                    if (x >= 10 && x <= 26) f[i] += f[i - 2];
                }
            }
        }
        return f[n - 1];
    }
};


活动打卡代码 LeetCode 90. 子集 II

optimjie
13天前
class Solution {
public:
    int n;
    vector<vector<int>> ans;
    vector<int> path;
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        this->n = nums.size();
        sort(nums.begin(), nums.end());
        dfs(0, nums);
        return ans;
    }
    void dfs(int u, vector<int>& nums) {
        if (u == n) {
            ans.push_back(path);
            return;
        }
        int k = u;
        while (k < n && nums[k] == nums[u]) k++;
        for (int i = 0; i <= k - u; i++) {
            dfs(k, nums);
            path.push_back(nums[u]);
        }
        for (int i = 0; i <= k - u; i++) path.pop_back();
    }
};


活动打卡代码 LeetCode 89. 格雷编码

optimjie
13天前
class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> res(1, 0);
        while (n--) {
            for (int i = res.size() - 1; i >= 0; i--) {
                res[i] *= 2;
                res.push_back(res[i] + 1);
            }
        }
        return res;
    }
};



optimjie
13天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummy = new ListNode(-1);
        ListNode* tail = dummy;
        for (ListNode *l = head, *r = head; l != NULL; ) {
            while (r != NULL && r->val == l->val) r = r->next;
            if (l->next == r) {
                tail->next = l;
                tail = l;
                tail->next = NULL;
            }
            l = r;
        }
        return dummy->next;
    }
};



optimjie
13天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *dummy = new ListNode(-1);
        ListNode *tail = dummy;
        for (ListNode *l = head, *r = head; l != NULL; ) {
            while (r != NULL && r->val == l->val) r = r->next;
            tail->next = l;
            tail = l;
            tail->next = NULL;
            l = r;
        }
        return dummy->next;
    }
};



optimjie
13天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *dummy = new ListNode(-1);
        ListNode *tail = dummy;
        for (ListNode *l = head, *r = head; l != NULL; ) {
            while (r != NULL && r->val == l->val) r = r->next;
            tail->next = l;
            tail = l;
            tail->next = NULL;
            l = r;
        }
        return dummy->next;
    }
};



optimjie
13天前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummy = new ListNode(-1);
        ListNode* tail = dummy;
        for (ListNode *l = head, *r = head; l != NULL; ) {
            while (r != NULL && r->val == l->val) r = r->next;
            if (l->next == r) {
                tail->next = l;
                tail = l;
                tail->next = NULL;
            }
            l = r;
        }
        return dummy->next;
    }
};