第 33 场双周赛

1556. 千位分隔数(找规律)

to_string()转换为字符串

class Solution {
public:
    string thousandSeparator(int n) {
        string num = to_string(n); // to_string() 转换为字符串
        string res;
        for(int i = num.size() - 1, j = 0; i >= 0 ; i-- ,j ++)
        { 
            if(j % 3 == 0 && j) res += '.';
            res += num[i];
        }
        reverse(res.begin(),res.end()); // 翻转字符串

        return res;
    }
};

1557. 可以到达所有点的最少点数目(找入度为0的点)

class Solution {
public:
    vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {
        // 统计入度为0的点
        vector<int> d(n);
        for(auto &e : edges)
        {
            int a = e[0], b = e[1];
            d[b] ++;
        }

        vector<int> res;
        for(int i=0;i<n;i++)
            if(!d[i])
                res.push_back(i);
        
        return res;
    }
};

1558. 得到目标数组的最少函数调用次数(从二进制角度分析,贪心题)

class Solution {
public:
    int minOperations(vector<int>& nums) {
        // 从二进制角度分析,贪心题
        int k = 0, o = 0; // k:最大位数, o:0的个数
        for(auto x : nums)
        {
            int bits = 0, ones = 0;
            while(x)
            {
                if(x & 1) ones ++;
                bits ++;
                x >>= 1;
            }
            k = max(k,bits), o += ones;
        }

        return k + o - 1;  // 从最大数分析,答案必然是最大位数 + 各个数二进制1的个数
    }   
};

1559. 二维网格图中探测环(无向图找环)

dfs1

class Solution {
public:

    vector<vector<char>> g;
    vector<vector<bool>> st;
    int n, m;

    bool containsCycle(vector<vector<char>>& grid) {
        g = grid, n = grid.size(), m = grid[0].size();
        st = vector<vector<bool>>(g.size(),vector<bool>(g[0].size()));

        for(int i = 0; i < n;i ++)
            for(int j = 0;j < m;j ++ )
                if(!st[i][j] && dfs(i,j,-1))
                    return true;
        
        return false;
    }

    int dx[4] = {-1,1,0,0}, dy[4] = {0,0,-1,1}; // 上下左右
    bool dfs(int x,int y,int p){ // p :来时的边,不能往回走
        st[x][y] = true;
        for(int i=0;i<4;i++)
            if(i != p){ 
                int a = x + dx[i], b = y + dy[i];
                if(a >= 0 && a < n && b >= 0 && b < m && g[a][b] == g[x][y]){
                    if(st[a][b]) return true;
                    if(dfs(a, b, i ^ 1)) return true; // i ^ 1就是反向边,0 ^ 1 = 1,1 ^ 0 = 0
                                                // 2 ^ 1 = 3, 3 ^ 1= 2; 不过要把方向定为上下左右?
                }
            }
        
        return false;
    }
};

dfs2 -------- 这种比较好理解,并且适应leetcode答题模式

const int N = 510;

int n, m;
vector<vector<char>> g;
bool st[N][N];

int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};

bool dfs(int x,int y,int lastx,int lasty)
{
    st[x][y] = true;

    for(int i=0;i<4;i++)
    {
        int a = x + dx[i], b = y + dy[i];

        // 非法情况
        if(a < 0 || a >= n || b < 0 || b >= m) continue;
        if(g[a][b] != g[x][y]) continue;
        if(a == lastx && b == lasty) continue;

        if(st[a][b]) return true;

        if(dfs(a,b,x,y)) return true;
    }
    return false;
}


class Solution {
public:
    bool containsCycle(vector<vector<char>>& grid) {
        g = grid;
        n = grid.size(), m = grid[0].size();

        memset(st,0,sizeof st);

        for(int i = 0;i < n ;i ++ )
            for(int j = 0; j < m;j ++ )
                if(!st[i][j] && dfs(i,j,-1,-1))
                    return true;
        
        return false;
    }
};

y总视频学习地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值