从零开始的刷LeetCode生活 第7期 71-80

在这里插入图片描述

class Solution {
public:
    string simplifyPath(string path) {
        path += '/';//在最后加入斜杆
        string res, s;
        for(auto c : path)
        {
            if(res.empty()) res += c;
            else if(c != '/') s += c; //一串字符
            else //右斜杆
            {
                if(s == "..") //   ../返回上一级目录
                {
                    if(res.size() > 1) 
                    {
                        res.pop_back(); //去掉上一级右斜杆
                        while(res.back() != '/')
                            res.pop_back();
                    }
                }
                else if(s != "" && s != ".") // ./当前目录
                    res += s + '/';
                s = "";
            }
        }
        if(res.size() > 1) res.pop_back(); //最后斜杆去掉
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int minDistance(string word1, string word2) {
        int n = word1.size(), m = word2.size();
        vector<vector<int>> f(n + 1, vector<int>(m + 1)); //初始化
        for(int i = 0; i <= n; i ++) f[i][0] = i;
        for(int j = 0; j <= m; j ++) f[0][j] = j;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= m; j ++)
            {
                if(word1[i - 1] == word2[j - 1]) f[i][j] = f[i - 1][j - 1];
                else f[i][j] = f[i - 1][j - 1] + 1;//替换操作                
                f[i][j] = min(f[i][j], f[i][j - 1] + 1);//添加word1或者删除word2操作
                f[i][j] = min(f[i][j], f[i - 1][j] + 1);//添加Word2或者删除word1操作                                
            }
        return f[n][m];
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        if(matrix.empty()) return;
        int n = matrix.size(), m = matrix[0].size();
        bool row0 = false, col0 = false;
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                if(matrix[i][j] == 0)
                {
                    if(i == 0) row0 = true;
                    if(j == 0) col0 = true;
                    matrix[i][0] = matrix[0][j] = 0;
                }
        for(int i = 1; i < n; i ++)
            for(int j = 1; j < m; j ++)
                if(matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;
        if(col0) for(int i = 0; i < n; i ++) matrix[i][0] = 0; //首列为0 遍历每一行 置为0 
        if(row0) for(int j = 0; j < m; j ++) matrix[0][j] = 0;
    }
};

在这里插入图片描述

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if(matrix.empty() || matrix[0].empty()) return false;
        int n = matrix.size(), m = matrix[0].size();
        int l = 0, r = n * m - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(matrix[mid / m][mid % m] >= target) r = mid;
            else l = mid + 1;
        }
        return matrix[l / m][l % m] == target;
    }
};

在这里插入图片描述

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int zero = 0, second = nums.size() - 1;
        for(int i = 0; i <= second; i ++)
        {
            while(nums[i] == 2 && i < second) swap(nums[i], nums[second --]);
            if(nums[i] == 0) swap(nums[i], nums[zero ++]);
        }
    }
};

在这里插入图片描述

class Solution {
public:
    string minWindow(string s, string t) {
        string res;
        unordered_map<char, int>S, T;
        for(auto c : t) T[c] ++;
        int total = T.size();
        int satisfy = 0; //当前满足了多少个字母
        for(int i = 0, j = 0; i < s.size(); i ++)
        {
            S[s[i]] ++;
            if(S[s[i]] == T[s[i]]) satisfy ++;
            while(S[s[j]] > T[s[j]]) S[s[j ++]] --;
            if(satisfy == total && (res.empty() || i - j + 1 < res.size()))
                res = s.substr(j, i - j + 1);
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> combine(int n, int k) {
        vector<int> path;
        dfs(path, 1, n, k);
        return res;
    }

    void dfs(vector<int>&path, int start, int n, int k) //当前方案 起始位置 n k可选个数
    {
        if(!k)
        {
            res.push_back(path);
            return;
        }
        for(int i = start; i <= n; i ++)
        {
            path.push_back(i);
            dfs(path, i + 1, n, k - 1);
            path.pop_back();
        }
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    vector<vector<int>> subsets(vector<int>& nums) {        
        dfs(nums, 0);
        return res;
    }

    void dfs(vector<int>&nums, int u)
    {
        res.push_back(path);
        for(int i = u; i < nums.size(); i ++)
        {
            path.push_back(nums[i]);
            dfs(nums, i + 1);
            path.pop_back();
        }
    }
};

在这里插入图片描述

class Solution {
public:
    int n, m;
    vector<vector<char>> board;
    vector<vector<bool>> st;
    string word;
    bool exist(vector<vector<char>>& _board, string _word) {
        board = _board, word = _word;
        n = board.size(), m = board[0].size();
        st = vector<vector<bool>>(n, vector<bool>(m));
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
            {
                if(board[i][j] == word[0])
                    if(dfs(i, j, 1))
                        return true;
            }
        return false;
    }

    bool dfs(int x, int y, int u)
    {
        if(u == word.size()) return true;
        st[x][y] = true;
        int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
        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 && !st[a][b] && board[a][b] == word[u])
            {
                if(dfs(a, b, u + 1))
                    return true;
            }
        }
        st[x][y] = false;
        return false;
    }
};

在这里插入图片描述

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int i = 0;
        for(auto x : nums)
            if(i < 2 || x > nums[i - 2])
                nums[i ++] = x;
        return i;
    }
};
发布了121 篇原创文章 · 获赞 33 · 访问量 7313

猜你喜欢

转载自blog.csdn.net/qq_42549254/article/details/103859408