LeetCode周赛156

1207. Unique Number of Occurrences

两次计数,比较出现次数是否唯一

class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        map<int, int> m;
        for (int i = 0; i < arr.size(); i++) {
            m[arr[i]]++;
        }
        vector<bool> marked(arr.size() + 1);
        for (auto p : m) {
            if (marked[p.second] == true)
                return false;
            marked[p.second] = true;
        }
        return true;
    }
};

1208. Get Equal Substrings Within Budget

双指针

class Solution {
public:
    int equalSubstring(string s, string t, int maxCost) {
        int nsize = s.size();
        int h = 0;
        int cnt = 0, re = 0;
        for (int i = 0; i < nsize; i++) {
            if (i > 0) {
                cnt -= abs(s[i - 1] - t[i - 1]);
            }
            if (i > h) {
                h = i;
                cnt = 0;
            }
            while (h < nsize && cnt + abs(s[h] - t[h]) <= maxCost) {
                cnt += abs(s[h] - t[h]);
                re = max(re, h - i + 1);
                h++;
            }
         } 
        return re;
    }
};

1209. Remove All Adjacent Duplicates in String II

栈模拟

class Solution {
public:
    string removeDuplicates(string s, int k) {
        stack<pair<char, int>> st;
        for (int i = 0; i < s.size(); i++) {
            if (st.empty() || st.top().first != s[i]) {
                st.push({s[i], 1});
            } else {
                st.push({s[i], st.top().second + 1});
            }
            while (!st.empty() && st.top().second == k) {
                for (int i = 0; i < k; i++)
                    st.pop();
            }
        }
        string re;
        while (!st.empty()) {
            re = st.top().first + re;
            st.pop();
        }
        return re;
    }
};

1210. Minimum Moves to Reach Target with Rotations

BFS最短路径

        int paths = 0, size = 0, r = grid.size(), c = grid[0].size();
        queue<array<int, 3>> q;
        q.push({0,0,1});
        while (!q.empty()) {
            size = q.size();
            while (size--) {
                auto& v = q.front();
                if (v[0] == r - 1 && v[1] == c -  2 && v[2] == 1 )
                    return paths;

                if (v[2] == 1) {
                    marked[v[0]][v[1]] = true;
                    marked[v[0]][v[1] + 1] = true;
                    if (v[0] + 1 < r && grid[v[0] + 1][v[1]] == 0 && grid[v[0] + 1][v[1] + 1] == 0){
                        if(!marked[v[0] + 1][v[1]] && !marked[v[0] + 1][v[1] + 1]) 
                            q.push({v[0] + 1, v[1], 1});
                        if (!marked1[v[0] + 1][v[1]] && !marked1[v[0] + 1][v[1] + 1])
                            q.push({v[0], v[1], 0});
                    }
                    if (v[1] + 2 < c && grid[v[0]][v[1] + 2] == 0 && !marked[v[0]][v[1] + 2])
                        q.push({v[0], v[1] + 1, 1});
                } else {
                    int r1 = v[0] + 1, c1 = v[1];
                    marked1[v[0] + 1][v[1]] = true;
                    marked1[v[0]][v[1]] = true;
                    if (v[1] + 1 < c && grid[v[0]][v[1] + 1] == 0 && grid[v[0] + 1][v[1] + 1] == 0){
                        if(!marked1[v[0]][v[1] + 1] && !marked1[v[0] + 1][v[1] + 1])
                            q.push({v[0], v[1] + 1, 0});
                        if (!marked[v[0]][v[1] + 1] && !marked[v[0] + 1][v[1] + 1])
                            q.push({v[0], v[1], 1});
                    }
                    if (v[0] + 2 < r && grid[v[0] + 2][v[1]] == 0 && !marked1[v[0] + 2][v[1]])
                        q.push({v[0] + 1, v[1], 0});
                }
                q.pop();
            }
            paths++;

        }
        return -1;
    }
    ```
    这个超时了。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值