牛客刷题日记【字符串的排列|矩阵最长递增路径|括号生成】

牛客刷题:字符串排列、矩阵路径与括号生成

BM58 字符串的排列

输入一个长度为 n 字符串,打印出该字符串中字符的所有排列,你可以以任意顺序返回这个字符串数组。

例如输入字符串ABC,则输出由字符A,B,C所能排列出来的所有字符串ABC,ACB,BAC,BCA,CBA和CAB。

思考:

对字符串进行深度优先搜索,在dfs时明确当前已遍历的序列以及避免在同层中出现相同的字符。

代码:

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return string字符串vector
     */
    
    vector<string> res;

    void dfx (string str, vector<int> pos, string cur) {
        if (cur.size() == str.size()) {
            res.push_back(cur);
            return;
        }

        string c;

        for (int i = 0; i < str.size(); i++)
        {
            bool flag = false;

            for (int j = 0;j < pos.size();j++)  if (pos[j] == i)    flag = true;
            for (int j = 0;j < c.size();j++)    if (str[i] == c[j]) flag = true;

            if (flag) continue;

            c.push_back(str[i]);

            string tmp = cur;
            tmp.push_back(str[i]);

            vector<int> poss = pos;
            poss.push_back(i);

            dfx(str,poss,tmp);

        }
        
    }
    
    vector<string> Permutation(string str) {
        // write code here

        vector<int> d;
        string ss;

        dfx(str,d,ss);

        return res;
    }
};

BM61 矩阵最长递增路径

给定一个 n 行 m 列矩阵 matrix ,矩阵内所有数均为非负整数。 你需要在矩阵中找到一条最长路径,使这条路径上的元素是递增的。并输出这条最长路径的长度。

这个路径必须满足以下条件:

\1. 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外。

\2. 你不能走重复的单元格。即每个格子最多只能走一次。

思考:

数组矩阵的本质上有向图,从数字较小的结点到较大的结点为有向箭头。从每个结点开始遍历深搜。

代码:

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 递增路径的最大长度
     * @param matrix int整型vector<vector<>> 描述矩阵的每个数
     * @return int整型
     */
    
    int res = -1;

    void dfx (vector<vector<int> >& matrix, int cur, int x, int y) {
        
        if (x + 1 < matrix.size()   && matrix[x+1][y] > matrix[x][y])   dfx(matrix, cur+1, x+1,y);
        if (x - 1 > -1              && matrix[x-1][y] > matrix[x][y])   dfx(matrix, cur+1, x-1,y);
        if (y + 1 < matrix[x].size()&& matrix[x][y+1] > matrix[x][y])   dfx(matrix, cur+1, x,y+1);
        if (y - 1 > -1              && matrix[x][y-1] > matrix[x][y])   dfx(matrix, cur+1, x,y-1);

        res = max(res, cur);
    }
    
    int solve(vector<vector<int> >& matrix) {
        // write code here

        for (int i = 0;i < matrix.size();i++)   for (int j = 0;j < matrix[i].size();j++)
            dfx(matrix, 0, i, j);

        return res+1;
    }
};

BM60 括号生成

给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合。

例如,给出n=3,解集为:

“((()))”, “(()())”, “(())()”, “()()()”, “()(())”

思考:

用递归回溯的方法求解。

首先明确递归终止条件,当字符串长度为n的2倍时停止,如果此时左右括号的数量形同,则进入最终字符串数组中。

接着依次向字符串中添加右左括号,并通过n1控制当前已匹配括号的数量。

代码:

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串vector
     */

    vector<string> res;

    void dfx (int n, int n1,string cur) {
        if (cur.size() == 2*n) {
            if (!n1) res.push_back(cur);
            return;
        }
        
        if (0 < n1 && n1 <= n) {
            string tmp = cur;
            tmp.push_back(')');
            std::cout << n1 << endl;
            // n1--;
            dfx(n, n1-1, tmp);
        }
        if (0 <= n1 && n1 < n) {
            string tmp = cur;
            tmp.push_back('(');
            std::cout << n1 << endl;
            // n1++;
            dfx(n, n1+1, tmp);
        }
    }
    
    vector<string> generateParenthesis(int n) {
        // write code here
        string s = "(";
        dfx(n,1,s);

        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值