从零开始的刷LeetCode生活 第40期 414-430

在这里插入图片描述

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        set<int> hash;//set里面会自动排序(从小到大)
        for(auto x : nums)
        {
            hash.insert(x);
            if(hash.size() > 3)
                hash.erase(*hash.begin());
        }
        if(hash.size() < 3)
            return *hash.rbegin();
        else
            return *hash.begin();
    }
};

在这里插入图片描述

class Solution {
public:
    string addStrings(string num1, string num2) {
        string res = "";
        int cur = 0, i = num1.size() - 1, j = num2.size() - 1;
        while(~i || ~j || cur != 0)
        {
            if(~i) cur += num1[i--] - '0';
            if(~j) cur += num2[j--] - '0';
            res += to_string(cur % 10);
            cur /= 10; 
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for(auto x : nums) sum += x;
        if(sum % 2 != 0) return false;
        sum /= 2;
        sort(nums.rbegin(), nums.rend());
        if(nums[0] > sum) return false;
        return dfs(nums, 0, 0, sum);
    }

    bool dfs(vector<int>&nums, int index, int now, int sum)
    {
        if(index > nums.size() - 1) return false;
        if(now + nums[index] == sum) return true;
        else if(now + nums[index] > sum) return dfs(nums, index + 1, now, sum);
        else return dfs(nums, index + 1, now + nums[index], sum) || dfs(nums, index + 1, now, sum);
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        vector<vector<int>> res;
        int n = matrix.size();
        if(n < 1) return res;
        int m = matrix[0].size();
        vector<vector<bool>> Pacific(n, vector<bool>(m, false));
        vector<vector<bool>> Atlantic(n, vector<bool>(m, false));        
        for(int i = 0; i < n; i ++) //循环列
        {
            dfs(matrix, i, 0, Pacific, matrix[i][0]); //左右
            dfs(matrix, i, m - 1, Atlantic, matrix[i][m - 1]);
        }        
        for(int i = 0; i < m; i ++)
        {
            dfs(matrix, 0, i, Pacific, matrix[0][i]); //上下    
            dfs(matrix, n - 1, i, Atlantic, matrix[n - 1][i]);
        }        
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                if(Pacific[i][j] && Atlantic[i][j])
                    res.push_back({i, j});
        return res;
    }
    //注意取地址符号 修改visit
    void dfs(vector<vector<int>>&m, int x, int y, vector<vector<bool>>&visit, int pre)
    {
        //cout << 1 ;
        if(x < 0 || y < 0 || x >= m.size() || y >= m[0].size() || visit[x][y] || m[x][y] < pre)
            return;        
        visit[x][y] = true;
        dfs(m, x + 1, y, visit, m[x][y]);
        dfs(m, x - 1, y, visit, m[x][y]);
        dfs(m, x, y + 1, visit, m[x][y]);        
        dfs(m, x, y - 1, visit, m[x][y]);    
    }
};

在这里插入图片描述

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int res = 0;
        for(int i = 0; i < board.size(); i ++)
            for(int j = 0; j < board[0].size(); j ++)
                if(board[i][j] == 'X')
                {
                    if(i > 0 && board[i - 1][j] == 'X' || j > 0 && board[i][j - 1] == 'X')
                        continue;
                    else res ++;
                }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int strongPasswordChecker(string s) {
        bool is_char, is_digit, is_bchar;
        is_char = is_digit = is_bchar = true;
        for(auto &c : s)
        {
            if(isdigit(c))
                is_digit = false;
            else if(islower(c))
                is_char = false;
            else if(isupper(c))
                is_bchar = false;
        }
        int isnum = is_digit + is_char + is_bchar;
        int ins = 0, ins2 = 0, rep = 0;
        for(int i = 2; i < s.size(); i ++)
        {
            if(s[i] == s[i - 1] && s[i - 1] == s[i - 2])
            {
                int len = 3;
                while(i + 1 < s.size() && s[i + 1] == s[i])
                {
                    i ++; len ++;
                }
                if(len % 3 == 0)
                    ins ++;
                else if(len % 3 == 1)
                    ins2 ++;
                rep += len / 3;
            }
        }
        if(s.size() < 6)
            return max(6 - (int)s.size(), isnum);
        else if(s.size() <= 20)
            return max(rep, isnum);
        else{
            int del = s.size() - 20;
            rep -= min(del, ins);
            if(del - ins > 0)
                rep -= min((del - ins) / 2, ins2);
            if(del - ins - 2 * ins2 > 0)
                rep -= (del - ins - 2 * ins2) / 3;
            return del + max(rep, isnum);
        }
    }
};

在这里插入图片描述

class Solution {
public:
    struct Node{
        int son[2];
    };
    vector<Node> nodes;
    int findMaximumXOR(vector<int>& nums) {
        nodes.push_back(Node({0, 0}));//初始化
        for(auto x : nums)
        {
            int p = 0;
            for(int i = 30; ~i; i --)
            {
                int t = x >> i & 1;
                if(!nodes[p].son[t])
                {
                    nodes.push_back(Node({0, 0}));
                    nodes[p].son[t] = nodes.size() - 1;
                }
                p = nodes[p].son[t];
            }
        }
        int res = 0;
        for(auto x : nums)
        {
            int p = 0, m_xor = 0;
            for(int i = 30; ~i; i --)
            {
                int t = x >> i & 1;
                if(nodes[p].son[!t]) //走相反方向
                {
                    p = nodes[p].son[!t];
                    m_xor += 1 << i;
                }
                else
                    p = nodes[p].son[t];
            }
            res = max(res, m_xor);
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string originalDigits(string s) {
        int hash[128] = {0};
        for(auto x : s)
            hash[x] ++;
        vector<int>tmp;
        string res = "";
        while(hash['z'] > 0)
        {
            hash['z'] --;hash['e'] --;
            hash['r'] --;hash['o'] --;
            tmp.push_back(0);
        }
        while(hash['w'] > 0)
        {
            hash['t'] --;hash['w'] --;
            hash['o'] --;
            tmp.push_back(2);
        }
        while(hash['u'] > 0)
        {
            hash['f'] --;hash['o'] --;
            hash['u'] --;hash['r'] --;
            tmp.push_back(4);
        }
        while(hash['x'] > 0)
        {
            hash['s'] --;hash['i'] --;
            hash['x'] --;
            tmp.push_back(6);
        }
        while(hash['g'] > 0)
        {
            hash['e'] --;hash['i'] --;
            hash['g'] --;hash['h'] --;
            hash['t'] --;
            tmp.push_back(8);
        }
        while(hash['h'] > 0)
        {
            hash['t'] --;hash['h'] --;
            hash['r'] --;hash['e'] --;
            hash['e'] --;
            tmp.push_back(3);
        }
        while(hash['f'] > 0)
        {
            hash['f'] --;hash['i'] --;
            hash['v'] --;hash['e'] --;
            tmp.push_back(5);
        }
        while(hash['s'] > 0)
        {
            hash['s'] --;hash['e'] --;
            hash['v'] --;hash['e'] --;
            hash['n'] --;
            tmp.push_back(7);
        }
        while(hash['i'] > 0)
        {
            hash['n'] --;hash['i'] --;
            hash['n'] --;hash['e'] --;
            tmp.push_back(9);
        }
        while(hash['o'] > 0)
        {
            hash['o'] --;hash['n'] --;
            hash['e'] --;
            tmp.push_back(1);
        }            
        sort(tmp.begin(), tmp.end());        
        for(auto c : tmp)
            res += to_string(c);        
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int characterReplacement(string s, int k) {
        vector<int>hash(26, 0);
        int left = 0, res = 0, maxCnt = 0;
        for(int i = 0; i < s.size(); i ++)
        {
            hash[s[i] - 'A'] ++;
            maxCnt = max(maxCnt, hash[s[i] - 'A']);
            while(i - left + 1 - maxCnt > k)
            {
                hash[s[left]- 'A']--;
                left ++;
            }
            res = max(res, i - left + 1);
        }
        return res;
    }
};

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

/*
// Definition for a QuadTree node.
class Node {
public:
    bool val;
    bool isLeaf;
    Node* topLeft;
    Node* topRight;
    Node* bottomLeft;
    Node* bottomRight;

    Node() {}

    Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = _topLeft;
        topRight = _topRight;
        bottomLeft = _bottomLeft;
        bottomRight = _bottomRight;
    }
};
*/
class Solution {
public:
    Node* construct(vector<vector<int>>& grid) {
        return dfs(grid, 0, 0, grid.size());
    }
    Node* dfs(vector<vector<int>>&grid, int x, int y, int size)
    {
        if(size <= 0) return 0;
        for(int i = x; i < x + size; i ++)
            for(int j = y; j < y + size; j ++)
            {
                if(grid[i][j] != grid[x][y])
                {
                    return new Node(false, false,
                    dfs(grid, x, y, size / 2),
                    dfs(grid, x, y + size / 2, size / 2),
                    dfs(grid, x + size / 2, y, size / 2),
                    dfs(grid, x + size / 2, y + size / 2, size / 2));
                }
            }
        return new Node(grid[x][y], true, nullptr, nullptr, nullptr, nullptr);
    }
};

在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> res;
        if(!root) return res;
        queue<Node*>q;
        q.push(root);
        while(!q.empty())
        {
            vector<int>t;
            for(int i = q.size(); i; i --)
            {
                Node* cur = q.front();
                q.pop();
                t.push_back(cur->val);
                for(Node* it : cur->children)
                    q.push(it);
            }
            res.push_back(t);
        }
        return res;
    }
};

在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* prev;
    Node* next;
    Node* child;
};
*/
class Solution {
public:
    Node* flatten(Node* head) {
        if(!head) return 0;
        Node* cur = head;
        while(cur)
        {
            if(cur->child)
            {
                Node *next = cur->next;
                Node *child = flatten(cur->child);
                cur->next = child;
                child->prev = cur;
                cur->child = NULL;
                if(next)
                {
                    while(cur->next)
                        cur = cur->next;
                    cur->next = next;
                    next->prev = cur;
                }
            }
            cur = cur->next;
        }
        return head;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值