栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母

leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母匹配字典里最长单词 Tips
  • 更多题解请见本系列目录
  • Java和C#核心代码完全一样, length() and Length, compareTo() and CompareTo(), for and foreach
  • 这题比较简单,遍历字符串数组后使用双指针比较目标字符s与字符串数组内的元素。
  • 在类内额外定义一个双指针的字符串是否属于子串的函数,代码逻辑结构会更清晰。
Python
class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        result = ''
        for item in dictionary:
            p_item = p_s = 0
            while p_itemlen(result) or (len(item)==len(result) and result>item):
                    result = item
        
        return result
C++
class Solution {
public:
    string findLongestWord(string s, vector& dictionary) {
        string longestWord = "";
        for(string target : dictionary)
        {
            int l1=longestWord.size(), l2=target.size();
            if(l1>l2 || (l1==l2 && longestWord.compare(target)<0)) continue;
            if(isSubstr(s, target)) longestWord = target;
        }
        return longestWord;
    }

private:
    bool isSubstr(string s, string target) {
        int i = 0, j = 0;
        while (i < s.length() && j < target.length()) {
            if (s[i] == target[j]) {
                j++;
            }
            i++;
        }
        return j == target.length();
    }

};
C#
public class Solution {
    public string FindLongestWord(string s, IList dictionary) {
        string longestWord = "";
        foreach(string target in dictionary)
        {
            int l1=longestWord.Length, l2=target.Length;
            if(l1>l2 || (l1==l2 && longestWord.CompareTo(target)<0)) continue;
            if(isSubstr(s, target)) longestWord = target;
        }
        return longestWord;
    }
    

private bool isSubstr(string s, string target) {
        int i = 0, j = 0;
        while (i < s.Length && j < target.Length) {
            if (s[i] == target[j]) {
                j++;
            }
            i++;
        }
        return j == target.Length;
    }
}
Java
class Solution {
    public String findLongestWord(String s, List d) {
        String longestWord = "";
        for (String target : d) {
            int l1 = longestWord.length(), l2 = target.length();
            if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
                continue;
            }
            if (isSubstr(s, target)) {
                longestWord = target;
            }
        }
        return longestWord;
    }

    private boolean isSubstr(String s, String target) {
        int i = 0, j = 0;
        while (i < s.length() && j < target.length()) {
            if (s.charAt(i) == target.charAt(j)) {
                j++;
            }
            i++;
        }
        return j == target.length();
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号