头像

NFYD




离线:59分钟前


最近来访(360)
用户头像
asdfasdfasdfa
用户头像
MH_6
用户头像
Const111
用户头像
西柚-
用户头像
Chenjiap69
用户头像
甜心还不会算法
用户头像
Sakura1
用户头像
bi_nian
用户头像
小红花
用户头像
蓝色幻想
用户头像
GG_boy
用户头像
少年补题王
用户头像
吴诗豪
用户头像
深情兹日
用户头像
WA声闹彻明
用户头像
junnnnnn
用户头像
兜里没有糖
用户头像
北边小洛
用户头像
情断青丝
用户头像
snkz5qing


NFYD
8天前
#include <iostream>

using namespace std;

typedef pair<int, int> PII;

int main()
{
    // 法一:初始化构造函数
    PII t1(1, 1);

    // 法二:std::make_pair函数
    PII t2 = make_pair(2, 2);

    // 法三:聚合初始化(要求C++11及以上)
    PII t3 = {3, 3};

    // 法四:直接使用列表初始化语法
    PII t4({4, 4});

    cout << t1.first << ' ' << t1.second << endl;
    cout << t2.first << ' ' << t2.second << endl;
    cout << t3.first << ' ' << t3.second << endl;
    cout << t4.first << ' ' << t4.second << endl;
    return 0;
}



NFYD
23天前

1.png

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

vector<int> state1;
stack<int> stk; // stk表示state2
int n, cnt = 20, state3 = 1;

void dfs()
{
    if (cnt == 0) return;
    // 情况1,state1表示已经出栈的序列
    if (state1.size() == n)
    {
        cnt -- ;
        for (auto x: state1) cout << x;
        cout << endl;
        return;
    }
    // 情况2,state2表示栈中序列
    if (stk.size())
    {
        state1.push_back(stk.top());
        stk.pop();
        dfs();

        // 下面两句都是恢复现场(是一个整体)
        stk.push(state1.back());
        state1.pop_back();
    }
    if (state3 <= n)
    {
        stk.push(state3);
        state3 ++ ;
        dfs();

        //恢复现场
        state3 -- ;
        stk.pop();
    }
}

int main()
{
    cin >> n;
    dfs();
    return 0;
}



NFYD
23天前
  • 42. 栈的压入、弹出序列 本质上一样,就是把数据类型从int换成了char
  • 思路:借助一个栈,扫描入栈序列,每次先把当前扫描到的数压入栈中,然后判断:当当前栈顶与出栈序列的第k(k从0开始)个元素相同时,就弹出栈顶,k++, 然后继续往后做,扫描完整个入栈序列时,如果栈为空,那么说明出栈序列合法,否则不合法
#include <iostream>
#include <stack>

using namespace std;

string s; // 出栈序列
string pushed = "abcdefghijklmnopqrstuvwxyz"; // 入栈序列
stack<char> stk;

int main()
{
    while (cin >> s)
    {
        stk = stack<char>();
        int k = 0;
        for (auto x: pushed)
        {
            stk.push(x);
            while (stk.size() && stk.top() == s[k] && k < s.size())
            {
                stk.pop();
                k ++ ;
            }
        }
        if (stk.empty()) puts("yes");
        else puts("no");
    }
    return 0;
}



NFYD
23天前
class Solution {
public:
    stack<int> stk;
    bool isPopOrder(vector<int>& pushed,vector<int>& popped) {
        if (pushed.size() != popped.size()) return false;
        int k = 0;
        for (auto x: pushed)
        {
            stk.push(x);
            while (stk.size() && stk.top() == popped[k])
            {
                stk.pop();
                k ++ ;
            }
        }
        return stk.empty();
    }
};


活动打卡代码 LeetCode 946. 验证栈序列

NFYD
23天前
class Solution {
public:
    stack<int> stk;
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int k = 0;
        for (auto x: pushed)
        {
            stk.push(x);
            while (stk.size() && stk.top() == popped[k])
            {
                stk.pop();
                k ++ ;
            }
        }
        return stk.empty();
    }
};



NFYD
24天前

Snipaste_2022-09-06_20-19-17.png

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    unordered_map<int, int> pos;

    TreeNode* build(vector<int>& preorder, vector<int>& postorder, int a, int b, int x, int y)
    {
        if (a > b) return NULL;
        auto root = new TreeNode(preorder[a]);
        if (a == b) return root;
        int k = pos[preorder[a + 1]];
        root->left = build(preorder, postorder, a + 1, a + 1 + k - x, x, k);
        root->right = build(preorder, postorder, a + 1 + k - x + 1, b, k + 1, y - 1);
        return root;
    }

    TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
        int n = preorder.size();
        for (int i = 0; i < n; i ++ ) pos[postorder[i]] = i;
        return build(preorder, postorder, 0, n - 1, 0, n - 1);
    }
};



NFYD
24天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sum;
    vector<int> path;
    int sumNumbers(TreeNode* root) {
        if (!root) return 0;
        sum = 0;
        dfs(root);
        return sum;
    }

    void dfs(TreeNode* root)
    {
        path.push_back(root->val);
        if (!root->left && !root->right)
        {
            string line;
            for (int i = 0; i < path.size(); i ++ )
                line += to_string(path[i]);
            sum += stoi(line);
        }
        else
        {
            if (root->left) dfs(root->left);
            if (root->right) dfs(root->right);
        }
        path.pop_back();
    }
};



NFYD
25天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<string> res;
    vector<int> path;
    vector<string> binaryTreePaths(TreeNode* root) {
        if (root) dfs(root);
        return res;
    }

    void dfs(TreeNode* root)
    {
        path.push_back(root->val);
        if (!root->left && !root->right)
        {
            string line = to_string(path[0]);
            for (int i = 1; i < path.size(); i ++ )
                line += "->" + to_string(path[i]);
            res.push_back(line);
        }
        else
        {
            if (root->left) dfs(root->left);
            if (root->right) dfs(root->right);
        }
        path.pop_back();
    }

};



NFYD
25天前

CCF-CSP考试历年真题题型分类

约瑟夫环

#include <iostream>
#include <queue>

using namespace std;

int n, k;

bool check(int x) // 如果出局则返回true
{
    if (x % k == 0 || (x % 10) == k) return true;
    return false;
}

int main()
{
    cin >> n >> k;
    queue<int> q;

    for (int i = 1; i <= n; i ++ ) q.push(i);

    int j = 1; // 从1开始报数
    while (q.size() > 1)
    {
        int t = q.front();
        q.pop();
        if (!check(j)) q.push(t); // 如果这个数不用出局,那就再加到队尾
        j ++ ;
    }
    cout << q.front() << endl;
    return 0;
}



NFYD
25天前
#include <iostream>

using namespace std;

const int N = 55;

int n;
int ne[N];

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        cin >> n;
        for (int i = 1; i < n; i ++ ) ne[i] = i + 1;
        ne[n] = 1;

        int p = n;
        for (int i = 0; i < n; i ++ )
        {
            p = ne[ne[p]]; // 走两步
            cout << ne[p] << ' ';
            ne[p] = ne[ne[p]]; // 删掉一个节点
        }
        cout << endl;
    }
    return 0;
}