头像

越狱


访客:390

离线:7个月前



越狱
8个月前

题目描述

blablabla

样例

blablabla

算法1

时间复杂度

o(n)

参考文献

C++ 代码

class Solution {
public:
    int longestSubstringWithoutDuplication(string s) {
        vector<int> v(256,-1);
        int start = -1;
        int res = 0;
        for(int i=0;i<s.size();i++)
        {
            if(v[s[i]]>start)
                start = v[s[i]];//change sliding window.
            v[s[i]]=i;
            res = max(res,i-start);    
        }
        return res;
    }
};




越狱
8个月前

C++ 代码

#include <iostream>
#include <stdlib.h>
#include  <cstring>

using namespace std;

struct Edge
{
    int to;
    int next;
};

const int N = 2e5 + 2;

int head[N],color[N];
Edge edge[N]; 
int cnt = 0;

void add(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

bool dfs(int u, int c)
{
    color[u] = c;
    for(int i=head[u]; i!=-1; i = edge[i].next)
    {
        int j = edge[i].to;
        if(color[j] == c)
            return false;
        if(!color[j] && !dfs(j,-c))
            return false;
    }
    return true;
}

int main()
{
    int n,m;
    cin>>n>>m;
    memset(head, -1, sizeof(head));
    memset(color, 0, sizeof(color));
    while(m--)
    {
        int u,v;
        cin>>u>>v;
        add(u,v);
        add(v,u);
    }
    for(int i=1; i<=n; ++i)
    {
        if(!color[i])
        {
            if(!dfs(i,1))
                {
                    cout<<"No"<<endl;
                    return 0;
                }
        }
    }
    cout<<"Yes"<<endl;
    return 0;
}




越狱
8个月前

class Solution {
public:
void dfs_s(TreeNode root, string &res)
{
if(!root)
{
res += “null “;
return;
}
res += to_string(root->val) + ‘ ‘;
dfs_s(root->left, res);
dfs_s(root->right, res);
}
// Encodes a tree to a single string.
string serialize(TreeNode
root) {
string res;
dfs_s(root, res);
//cout<<res<<endl;
return res;
}

TreeNode* dfs_d(string s, int& u)
{
    if(u == s.size())
        return NULL;
    int k = u;
    while(s[k] != ' ')
    {
        k++;
    }
    if(s[u] == 'n')
    {
        u = k + 1;
        return NULL;
    }
    int val = 0;
    for(int i=u; i<k; ++i)
    {
        if(s[u] == '-')// 改成s[i] == '-'就没有问题,不是一样的吗?
            i++;
        val = val * 10 + s[i] - '0';
    }
    if(s[u] == '-')
        val = (-1) *  val;
    u = k+1;
    auto node = new TreeNode(val);
    node->left = dfs_d(s, u);
    node->right = dfs_d(s, u);
    return node;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
    int u = 0;
    return dfs_d(data, u);
}

};

测试数据: [-10, 13, -1, null, null, 6, null, 2, null, null, null]
结果:
[6, 13, -1, null, null, 6, null, 2, null, null, null]