[leetcode]720. Longest Word in Dictionary字典中最长的单词

b.compareTo(a)

这个函数是比较两个值得大小,如果b比a大,那么返回1

如果小,那么返回-1,相等返回0

如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1

这个题的核心虽然是hashtable,但是这个方法还是很重要的,因为自己实现比较字符串很麻烦

/*
    用一个Hashset来判断子串是不是在集合里
    然后遍历出最长的就行
    长度相同的用compareTo方法判断谁的字典顺序靠前
    这个题最难的感觉其实是不知道compareTo方法,自己实现还挺麻烦的
     */
    public String longestWord(String[] words) {
        //构建set
        Set<String> set = new HashSet<>();
        for (String s :
                words) {
            set.add(s);
        }
        //记录最大长度字符串
        String res = "";
        //遍历判断
        for (String s :
                words) {
            //两种情况值得更新,一种是长度更长,一种是长度一样但是顺序靠前
            if (s.length() > res.length() || (s.length() == res.length() && s.compareTo(res) < 0)) {
                //判断字符串的子串在不在集合里
                boolean judge = true;
                for (int i = s.length() - 1; i > 0; i--) {
                    if (!set.contains(s.substring(0, i))) {
                        judge = false;
                        break;
                    }
                }
                if (judge)
                    res = s;
            }
        }
        return res;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8343352.html

时间: 2024-08-29 21:22:46

[leetcode]720. Longest Word in Dictionary字典中最长的单词的相关文章

leetcode 720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[LeetCode] 524. Longest Word in Dictionary through Deleting

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographic

[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographic

【C】字符串的输入,求输入字符串中最长的单词

首先,基本目标很简单,就是利用C语言:编写一个函数,输入一行字符,将此行字符中的最长的单词输出. 代码如下: #include<stdio.h> void input(char s[]){ int i=0; for(int c;(c=getchar())!='\n';i++){ s[i]=c; } s[i]='\0';//读取完成,记得对这个字符数组封口 } char* findmax(char s[]){ int max=0,word_length=0,p=0,i=0;//这个p是用来记录最

[LeetCode] Longest Word In Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

Hash Table-720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

524. Longest Word in Dictionary through Deleting

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions An alternate, more efficient solution which avoids sorting the dictionary: public String findLongestWord(String s, List<String> d) { String longest = "";

[LeetCode] Longest Mountain in Array 数组中最长的山

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

[LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst