522 Longest Uncommon Subsequence II 最长特殊序列 II
Posted lina2014
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了522 Longest Uncommon Subsequence II 最长特殊序列 II相关的知识,希望对你有一定的参考价值。
详见: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的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode Longest Uncommon Subsequence II
#Leetcode# 521. Longest Uncommon Subsequence I
Longest Uncommon Subsequence I
Longest Uncommon Subsequence I