从零开始的刷LeetCode生活 第13期 131-140

在这里插入图片描述

class Solution {
public:
    vector<vector<string>> res;
    vector<string> path;
    vector<vector<string>> partition(string s) {
        dfs("", 0, s);
        return res;
    }

    bool check(string & now)
    {
        if(now.empty()) return false;
        for(int i = 0, j = now.size() - 1; i < j; i ++, j --)
            if(now[i] != now[j])
                return false;
        return true;
    }

    void dfs(string now, int u, string &s)
    {
        if(u == s.size())
        {
            if(check(now))
            {
                path.push_back(now);
                res.push_back(path);
                path.pop_back();
            }
            return;
        }
        if(check(now))
        {
            path.push_back(now);
            dfs("", u, s);
            path.pop_back();
        }
        dfs(now + s[u], u + 1, s);
    }
};

在这里插入图片描述

class Solution {
public:
    int minCut(string s) {
        int n = s.size();
        vector<int> f(n + 1);
        vector<vector<bool>> st(n, vector<bool>(n, false));

        for(int i = 0; i < n; i ++)
            for(int j = i; ~j; j --)
                if(i - j <= 1) st[j][i] = s[j] == s[i];
                else st[j][i] = s[j] == s[i] && st[j + 1][i - 1];
        f[0] = 0;
        for(int i = 1; i <= n; i ++)
        {
            f[i] = INT_MAX;
            for(int j = 0; j < i; j ++)
                if(st[j][i - 1])
                    f[i] = min(f[i], f[j] + 1);
        }
        return max(0, f[n] - 1);
    }
};

在这里插入图片描述

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

    Node() {}

    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};
*/
class Solution {
public:
    unordered_map<Node*, Node*> hash;
    Node* cloneGraph(Node* node) {
        if(!node) return node;

        //创建新点
        auto p = new Node(node->val);
        hash[node] = p;

        dfs(node);
        return p;
    }

    void dfs(Node* node) //遍历整个图
    {
        for(auto ver : node->neighbors) //遍历邻接节点
        {
            if(!hash.count(ver)) //没有节点 先创建
            {
                hash[ver] = new Node(ver->val);
                dfs(ver);
            }
            hash[node]->neighbors.push_back(hash[ver]);
        }
    }
};

在这里插入图片描述

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();
        for(int i = 0, j; i < n; i += j + 1) //i += j + 1
        {
            int gas_left = 0;
            for(j = 0; j < n; j ++)
            {
                int k = (i + j) % n;
                gas_left += gas[k] - cost[k];
                if(gas_left < 0) break;
            }
            if(j >= n) return i;
        } 
        return -1;
    }
};

在这里插入图片描述

class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size(), res = 0;
        if(n < 2) return n;
        vector<int> f(n, 1), g(n, 1);
        for(int i = 1; i < n; i ++)
            if(ratings[i - 1] < ratings[i])
                f[i] = f[i - 1] + 1;
        for(int i = n - 2; ~i; i --)
            if(ratings[i] > ratings[i + 1])
                g[i] = g[i + 1] + 1;
        for(int i = 0; i < n; i ++)
            res += max(f[i], g[i]);
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for(auto x : nums) res ^= x;
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for(int bit = 0; bit < 32; bit ++)
        {
            int count = 0;
            for(int i = 0; i < nums.size(); i ++)
                count += (nums[i] >> bit) & 1;//统计第bit位 1 的个数
            res += (count % 3) << bit;
        }
        return res;
    }
};

在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head) return head;
        Node *p = head, *tempP;
        while(p)
        {
            tempP = new Node(p->val, p->next, p->random);
            tempP->next = p->next;
            p->next = tempP;
            p = p->next->next;
        }
        //复制random
        p = head;
        while(p)
        {
            if(p->random)
            {
                p->next->random = p->random->next;
            }
            else
                p->next->random = NULL;
            p = p->next->next;
        }
        p = head;
        Node* copyHead = p->next;
        while(p)
        {
            tempP = p->next;
            p->next = p->next->next;
            if(tempP->next)
                tempP->next = tempP->next->next;
            p = p->next;
        }
        return copyHead;
    }
};

在这里插入图片描述

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string>S;
        for(auto word : wordDict) S.insert(word);

        int n = s.size();
        vector<bool> f(n + 1, false);
        f[0] = true;

        for(int i = 1; i <= n; i ++)
            for(int j = 0; j < i; j ++)
                if(S.count(s.substr(j, i - j)) && f[j])
                {
                    f[i] = true;
                    break;
                }
        return f[n];
    }
};

在这里插入图片描述

class Solution {
public:
    vector<string>res;
    unordered_map<string, int>dict;
    vector<string> wordBreak(string s, vector<string>& wordDict) {
        for(auto &word : wordDict) dict[word] = 1;
        int n = s.size();
        vector<bool> f(n + 1, true);
        for(int i = 1; i <= n; i ++)
        {
            f[i] = false;
            for(int j = 0; j < i; j ++)
                if(dict[s.substr(j, i - j)] && f[j])
                {
                    f[i] = true;
                    break;
                }
        }
        dfs(f, s, "", n);
        return res;
    }

    void dfs(vector<bool>&f, string &s, string path, int u)
    {
        if(!u)
        {
            res.push_back(path.substr(0, path.size() - 1));//去除path最后面空格
            return;
        }
        for(int i = 0; i < u; i ++)
            if(dict[s.substr(i, u - i)] && f[i])
                dfs(f, s, s.substr(i, u - i) + " " + path, i);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值