详见: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. 获取IOS应用安装列表

    原文转载至 http://blog.csdn.net/justinjing0612/article/details/8887747 转自鸟哥博客:http://blog.cnrainbird.com/ ...

  2. weblogic 修改控制台console访问路径 url

    出于安全的考虑需要对weblogic的console进行屏避,或者修改默认的访问路径,主要有两种方法:(这里针对weblogic8.1) 一.进入默认的控制台,例如“localhost/console ...

  3. hive impala C++ Java垃圾回收 Garbage Collection GC

    hive impala impala  推荐每个节点内存  2^7~2^8GB Impala与Hive的比较 - 文章 - 伯乐在线 http://blog.jobbole.com/43233/ &l ...

  4. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  5. 3种方法判断手机浏览器跳转WAP手机网站

    随着移动设备的普及,企业的网络宣传已经不能局限在PC端,而需要同时在移动端有所建树.对于公司网站来说,以前都是做的PC端的,当然手机等移动端也可以访问,但是用户体验肯定不如完全适合的手机端来的方便.我 ...

  6. Web.xml配置----字符过滤器

    添加EncodingFilter类实现Filter接口 import javax.servlet.*;import javax.servlet.http.HttpServletRequest;impo ...

  7. C++数组作为函数参数的几个问题(转)

    本文需要解决C++中关于数组的2个问题:1. 数组作为函数参数,传值还是传址?2. 函数参数中的数组元素个数能否确定? 先看下面的代码. #include <iostream> using ...

  8. BDB c++例子,从源码编译到运行

    第一步先下载源码,解压后 ./dist/configure --enable-cxx编译,然后make, make install --enable-cxx To build the Berkeley ...

  9. 并不对劲的bzoj5475:loj2983:p5206:[wc2019]数树

    题目大意 task0:有两棵\(n\)(n\leq10^5)个点的树\(T1,T2\),每个点的点权可以是一个在\([1,y]\)里的数,如果两个点既在\(T1\)中有直接连边,又在\(T2\)中有直 ...

  10. AutoIt:获取计算机已安装程序列表

    $file = FileOpen(@ScriptDir&"\RegInstalledItems.csv",1) if $file = -1 Then ConsoleWrit ...