zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【C++ 系列笔记】03 C++ 面向对象进阶
    【C++ 系列笔记】02 C++ 面向对象基础
    【C++ 系列笔记】01 C++ 与 C
    【JavaScript】简单取随机数 ~~(Math.random() * number)
    英语测试
    Linux指令入门
    RE-攻防世界 T3 insanity
    PWN-攻防世界 level0
    ISCC不会的理论题
    kali linux配置ssh
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8909054.html
Copyright © 2011-2022 走看看