详见: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. bash shell parameter expansion

    1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...

  2. Immutable学习及 React 中的实践

    为什么用immutable.js呢.有了immutable.js可以大大提升react的性能. JavaScript 中的对象一般是可变的(Mutable),因为使用了引用赋值,新的对象简单的引用了原 ...

  3. mysql 发生系统错误1067

    一般是由配置文件错误语法不正确引起的,如my.ini本人在mysql mysql-5.6.29-winx64 配置过程中遇到“发生系统错误1067”主要由于下面两个目录写的格式不正确引起的正确写法如下 ...

  4. 动态预览Xib的实现

    写一个TestView继承于UIView,然后写个对应的xib,把xib的名字设置成TestView,这是标准的用xib加载这个view必须得条件 然后xib里把这个View的backgroundCo ...

  5. angularJS ng-if的用法

    ng-if主要是用来判断是否显示,也可以做为而者选择其中一个的方法,满足判断条件ng-if="变量名" 显示,否者不显示,也可以用ng-if="!变量名"取反, ...

  6. 你真的懂redis吗?

    Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行各种刁难.作为一名在互联网技术行业打击过成百上千名[请允许我夸张一下]的资深技术面试官 ...

  7. bzoj 4289 TAX —— 点边转化

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 把边转化成点,同一个原有点相连的边中,边权小的向大的连差值的边,大的向小的连0的边: ...

  8. java利用Aspose.words.jar将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)

    package doc; import java.io.*; import junit.framework.Test; import com.aspose.words.*; public class ...

  9. HTTP错误code大全

    100 - Continue 101 - Switching Protocols Top Success Codes 200 - OK 201 - Created 202 - Accepted 203 ...

  10. CodeForces 712A Memory and Crow (水题)

    题意:有一个序列,然后对每一个进行ai = bi - bi + 1 + bi + 2 - bi + 3.... 的操作,最后得到了a 序列,给定 a 序列,求原序列. 析:很容易看出来,bi = ai ...