头像

George




离线:47分钟前



George
9天前
#include<iostream>
using namespace std;


int const N = 1e6+10;
int q[N];
int n;

void quick_sort(int q[], int l, int r){
    if(l >= r) return ;

    int i = l-1, j = r+1;
    int x = q[r+l+1>>1];
    while(i < j){
        do i++; while(q[i] < x);
        do j--; while(q[j] > x);
        if(i < j) swap(q[i], q[j]);
    }

    quick_sort(q, l, i-1);
    quick_sort(q, i, r);
}

int main(){

    scanf("%d", &n);

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

    quick_sort(q, 0, n-1);

    for(int i = 0; i < n; i++) printf("%d ", q[i]);
}








George
12天前
/**
 * 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<vector<int>> res;
    vector<vector<int>> findPath(TreeNode* root, int sum) {
        if(!root) return res;
        dfs(root, sum, vector<int>{});

        return res;
    }

    void dfs(TreeNode *node, int sum, vector<int> path){
        path.push_back(node->val);
        sum -= node->val;
        if(!node->left && !node->right && !sum) res.push_back(path);

        if(node->left) dfs(node->left, sum, path);
        if(node->right) dfs(node->right, sum, path);
        path.pop_back();

    }
};



George
12天前
class Solution {
public:
    vector<int> seq;
    bool verifySequenceOfBST(vector<int> sequence) {
        seq = sequence;
        return dfs(0, seq.size()-1);
    }

    bool dfs(int l, int r){
        if(l >= r) return true;

        int root = seq[r];
        int k = l;
        while(k < r && seq[k]<root) k++;
        for(int i = k; i < r; i++)
            if(seq[i] < root)
                return false;

        return dfs(l, k-1) && dfs(k, r-1);
    }
};



George
12天前
/**
 * 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<vector<int>> res;
    vector<vector<int>> printFromTopToBottom(TreeNode* root) {
        if(!root) return res;
        dfs(root, 0);

        return res;
    }


    void dfs(TreeNode *&node, int level){
        if(level == res.size()) res.push_back(vector<int>{});
        res[level].push_back(node->val);

        if(node->left) dfs(node->left, level+1);
        if(node->right) dfs(node->right, level+1);
    }
};



George
12天前
/**
 * 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> res;
    vector<int> printFromTopToBottom(TreeNode* root) {
        if(root == NULL) return res;
        queue<TreeNode*> q;
        q.push(root);

        while(q.size()){
            auto p = q.front();
            q.pop();
            res.push_back(p->val);

            if(p->left) q.push(p->left);
            if(p->right) q.push(p->right);
        }

        return res;
    }
};



George
12天前
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> res;
        if(!matrix.size() || !matrix[0].size()) return res;
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int>> st(n, vector<int>(m));

        int dx[4]={-1, 0, 1, 0}, dy[4]={0, 1, 0, -1}, d = 1;
        int x = 0, y = 0;
        for(int k = 0; k < n*m; k++){
            res.push_back(matrix[x][y]);
            st[x][y] = true;
            int a = x+dx[d], b = y+dy[d];
            if(a<0 || a>=n || b<0 || b>=m || st[a][b]){
                d = (1+d)%4;
                a = x+dx[d], b = y+dy[d]; 
            }

            x = a, y = b;
        }


        return res;
    }
};



George
12天前
/**
 * 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:
    bool isSymmetric(TreeNode* root) {
        if(!root) return true;
        auto p = root->left, q = root->right;
        return isSame(p, q);
    }

    bool isSame(TreeNode *p, TreeNode *q){
        if(!p && !q) return true;
        if(!p || !q || p->val!=q->val) return false;
        return isSame(p->left, q->right) && isSame(p->right, q->left);
    }
};



George
12天前
/**
 * 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:
    bool hasSubtree(TreeNode* p, TreeNode* q) {
        if(!p || !q) return false;  
        if(contain(p, q)) return true;
        return hasSubtree(p->left, q) || hasSubtree(p->right, q);
    }

    bool contain(TreeNode* p, TreeNode* q){

        if(!q) return true;
        if(!p || p->val != q->val) return false;

        return contain(p->left, q->left) && contain(p->right, q->right);
    }
};


活动打卡代码 LeetCode 67. 二进制求和

George
14天前
class Solution {
public:
    string addBinary(string a, string b) {
        string res;
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());

        int k = 0;
        int t = 0;
        while(k < a.size() || k < b.size() || t){
            if(k < a.size()) t += a[k]-'0';
            if(k < b.size()) t += b[k]-'0';
            res += t%2 + '0';
            t >>= 1;
            k++;
        }


        reverse(res.begin(), res.end());

        return res;
    }
};


活动打卡代码 LeetCode 66. 加一

George
14天前
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int t = 1;

        for(int i = digits.size()-1; i >= 0; i--){
            t += digits[i];
            digits[i] = t%10;
            t /= 10;
        }
        if(t) digits.insert(digits.begin(), t);

        return digits;
    }
};