题目:给一个词典,找出其中所有最长的单词。

题目链接地址为:http://www.lintcode.com/zh-cn/problem/longest-words/#

给一个词典,找出其中所有最长的单词。
样例:
在词典
 {
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}
 
中, 最长的单词集合为 ["internationalization"]
 
在词典
 {
  "like",
  "love",
  "hate",
  "yes"
}
 
中,最长的单词集合为 ["like", "love", "hate"]

 代码如下:

package com.ak.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
	给一个词典,找出其中所有最长的单词。
	样例
在词典
 {
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}
 
中, 最长的单词集合为 ["internationalization"]
 
在词典
 {
  "like",
  "love",
  "hate",
  "yes"
}
 
中,最长的单词集合为 ["like", "love", "hate"]


 * @author Administrator
 *
 */
public class LongestWordsDemo {

	public static void main(String[] args) {
		/*String[] str = {
				  "dog",
				  "google",
				  "facebook",
				  "internationalization",
				  "blabla"
				};*/
		/*String[] str = {
				  "like",
				  "love",
				  "hate",
				  "yes"
				};*/
		String[] str = {
				 "dog",
				  "faceboon",
				  "google",
				  "facebook",
				  "blabla"
		};
		
		List<String> list = longestWords(str);
		System.out.println(list.toString());
	}
	
    public static List<String> longestWords(String[] dictionary) {
    	List<String> list = new ArrayList<String>();
    	Map<String,String> map = new HashMap<String,String>();
    	//循环遍历字符串数组
    	for(int i = 0;i < dictionary.length; i++){
    		//判断数组是否越界
    		if((i+1) < dictionary.length){
    			//比较两个数据的长度大小,如果较大的则用map记录下来
    			if(dictionary[i].length() > dictionary[i + 1].length()){
    				map.put(dictionary[i], dictionary[i]);
        		}else if(dictionary[i].length() == dictionary[i + 1].length()){//如果较大的和另外一个比较一样大则记录下来
        			map.put(dictionary[i], dictionary[i]);
        		}
    		}
    	}
    	//循环迭代map
    	for (Entry<String, String> entry: map.entrySet()) {  
    	   String value = entry.getValue();
    	   list.add(value);
    	}
		return list;
    }
}

代码写的不是很好,有好的意见大家可以提出,我可以学习学习一下。谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Trie树来解决这个问题。我们可以将这N个英文单词插入到Trie树中,然后在Trie树上搜索包含给定字符串的所有单词。 具体地,我们可以从给定字符串的第一个字符开始,在Trie树上进行深度优先搜索。每到达一个节点,我们就检查该节点是否表示一个单词,如果是,则将该单词加入到结果列表中。然后,我们继续向下搜索,直到搜索完整个字符串。 下面是使用Python实现的代码: ```python class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.is_word = True def search(self, word): node = self.root for ch in word: if ch not in node.children: return [] node = node.children[ch] return self._dfs(node, word) def _dfs(self, node, prefix): res = [] if node.is_word: res.append(prefix) for ch in node.children: res.extend(self._dfs(node.children[ch], prefix + ch)) return res # 示例 words = ["apple", "banana", "orange", "pineapple"] trie = Trie() for word in words: trie.insert(word) prefix = "app" res = trie.search(prefix) print(res) # ['apple'] ``` 在上面的代码中,我们首先定义了一个TrieNode类和一个Trie类。TrieNode类表示Trie树的一个节点,包含一个字典children,用来存储子节点,以及一个布尔变量is_word,用来表示该节点是否表示一个单词。Trie类包含一个根节点root,以及insert方法和search方法。 insert方法用来将一个单词插入到Trie树中。我们从根节点开始遍历单词的每个字符,如果当前字符不在当前节点的子节点中,则创建一个新的节点,并将其添加到子节点中。最终,我们将最后一个节点标记为单词的结尾。 search方法用来查包含给定字符串的所有单词。我们从根节点开始遍历字符串的每个字符,如果当前字符不在当前节点的子节点中,则说明不存在以该字符串为前缀的单词,直接返回空列表。否则,我们继续向下搜索。如果当前节点表示一个单词,则将该单词加入到结果列表中。然后,我们继续向下搜索,直到搜索完整个字符串。最后,返回结果列表。 在示例中,我们首先创建一个包含4个单词的Trie树。然后,我们搜索以"app"为前缀的所有单词,得到结果["apple"]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值