详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/

C++:

方法一:

class Solution {
public:
int findLUSlength(vector<string>& strs)
{
int res = -1, j = 0, n = strs.size();
for (int i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
if (i == j)
{
continue;
}
if (checkSubs(strs[i], strs[j]))
{
break;
}
}
if (j == n)
{
res = max(res, (int)strs[i].size());
}
}
return res;
}
int checkSubs(string subs, string str)
{
int i = 0;
for (char c : str)
{
if (c == subs[i])
{
++i;
}
if (i == subs.size())
{
break;
}
}
return i == subs.size();
}
};

方法二:

class Solution {
public:
int findLUSlength(vector<string>& strs)
{
int n = strs.size();
unordered_set<string> s;
sort(strs.begin(), strs.end(), [](string a, string b)
{
if (a.size() == b.size())
{
return a > b;
}
return a.size() > b.size();
});
for (int i = 0; i < n; ++i)
{
if (i == n - 1 || strs[i] != strs[i + 1])
{
bool found = true;
for (auto a : s)
{
int j = 0;
for (char c : a)
{
if (c == strs[i][j])
{
++j;
}
if (j == strs[i].size())
{
break;
}
}
if (j == strs[i].size())
{
found = false;
break;
}
}
if (found)
{
return strs[i].size();
}
}
s.insert(strs[i]);
}
return -1;
}
};

参考:http://www.cnblogs.com/grandyang/p/6680548.html

522 Longest Uncommon Subsequence II 最长特殊序列 II的更多相关文章

  1. 521 Longest Uncommon Subsequence I 最长特殊序列 Ⅰ

    给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列).子序列可以通过删去字符串中的某些字符实现,但不能改变剩余 ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  4. [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  5. 【leetcode】522. Longest Uncommon Subsequence II

    题目如下: 解题思路:因为given list长度最多是50,我的解法就比较随意了,直接用一个嵌套的循环,判断数组中每个元素是否是其他的subsequence,最后找出不属于任何元素subsequen ...

  6. LeetCode OJ:Longest Increasing Subsequence(最长递增序列)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. 521. Longest Uncommon Subsequence I 最长不同子数组

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: [一句话思路]: 两个单词的话,就是看谁 ...

  8. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

  9. [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

随机推荐

  1. 4408: [Fjoi 2016]神秘数

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 452  Solved: 273 [Submit][Stat ...

  2. kill 挂起 Apache Web Server

    [root@hadoop1 ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8 ...

  3. RSA前端JS加密,后端JAVA解密实现

    用RSA非对称加密方式实现.后台生成rsa密钥对,然后在页面设置rsa公钥,提交时用公钥加密密码,生成的密文传到后台,后台再用私钥解密,获取密码明文.这样客户端只需要知道rsa加密方式和公钥,前台不知 ...

  4. ie下文件上传无权访问的问题

    最近项目遇到个问题,ie下文件上传无权访问,在网上找了很久才找到答案,原来是因为ie下不能用js触发input=file的点击事件,必须手动点击才可以.

  5. PM12条

    PM首先是用户 站在用户角度看待问题 用户体验是一个完整的过程 追求效果,不做没用的东西 发现需求,而不是创造需求 决定不做什么,往往比决定做什么更重要 用户是很难被教育的,要迎合用户,而不是改变用户 ...

  6. spring cloud-服务注册

    正常的服务模块,注册到注册中心,让别的服务发现,调用服务 创建“服务提供方” 下面我们创建提供服务的客户端,并向服务注册中心注册自己. 假设我们有一个提供计算功能的微服务模块,我们实现一个RESTfu ...

  7. Easy smart REST with kbmMW

    使用新版kbmMW开发的 smart service,也可以轻松的发布为通过REST来调用的功能.   一个 kbmMW smart service象下面这样实现,就可以使用REST来访问:   ty ...

  8. flywaydb and sql server

    https://flywaydb.org/documentation/database/sqlserver How Flyway works https://flywaydb.org/getstart ...

  9. 织梦dedecms内页分类频道友情链接实现方法

    本文介绍了织梦dedecms中内页分类频道加友情链接的三种方法,有需要的朋友参考下. 织梦dedecms中内页分类频道加友情链接,方法有三种: 先讲方法,后讲原理: 方法:先找到首页模版index.h ...

  10. sublime text3 3176激活

    更改hosts sudo vim /etc/hosts 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 输入激活码 ---- ...