给你一个字符串和字典,从头扫到位,如果到当前的字符的字符串存在于字典中,则显示 buzz.

例子:

ILOVEPINEAPPLEJUICE

字典:

[pine, apple, pineapple, juice, applejuice]

那么当我们到达ILOVEPINE的时候,就要buzz,当我们到达APPLE的时候,也要buzz,当我们到达JUICE的时候,也要buzz.

 public class Solution {

     public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
trie.insert("pie");
trie.insert("applejuice");
trie.insert("juice"); Solution s = new Solution();
s.buzz("applepiejuice", trie.root);
} public void buzz(String str, Node root) {
List<Node> list = new ArrayList<Node>();
List<Node> temp = new ArrayList<Node>();
list.add(root);
for (int i = ; i < str.length(); i++) {
for (Node node : list) {
Node child = node.getChildNode(str.charAt(i));
if (child != null) {
temp.add(child);
if (child.isEnd) {
System.out.println("buzz");
}
}
}
if (i != ) {
Node child = root.getChildNode(str.charAt(i));
if (child != null) {
temp.add(child);
if (child.isEnd) {
System.out.println("buzz");
}
}
} list = temp;
temp = new ArrayList<Node>();
} }
} class Trie {
Node root; public Trie() {
root = new Node(' ');
} public void insert(String word) {
if (exists(word))
return;
Node current = root;
for (int i = ; i < word.length(); i++) {
char ch = word.charAt(i);
Node node = current.getChildNode(ch);
if (node == null) {
current.map.put(ch, new Node(ch));
current = current.getChildNode(ch);
} else {
current = node;
}
}
current.isEnd = true;
} public boolean exists(String word) {
Node current = root;
for (int i = ; i < word.length(); i++) {
char ch = word.charAt(i);
current = current.getChildNode(ch);
if (current == null) {
return false;
}
}
if (current.isEnd) {
return true;
} else {
return false;
}
} } class Node {
char ch;
boolean isEnd;
Map<Character, Node> map; public Node(char ch) {
this.ch = ch;
map = new HashMap<Character, Node>();
} public Node getChildNode(char ch) {
return map.get(ch);
}
}

Buzz words的更多相关文章

  1. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  2. 412. Fizz Buzz

    https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...

  3. Lintcode 9.Fizz Buzz 问题

    ------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...

  4. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  5. LeetCode之412. Fizz Buzz

    -------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public cl ...

  6. LeetCode Fizz Buzz

    原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...

  7. Fizz Buzz

    class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...

  8. LintCode (9)Fizz Buzz

    下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...

  9. [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式

    写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...

随机推荐

  1. yii2接收activeform表单信息

    第一种方法: $model->load(Yii::$app->request->post()); 第二种方法: 获取具体的某一项的值 $demo = $_POST['模型名']['字 ...

  2. echo '.SUFFIXES: .cpp' >> ${OUTPUT_FILE}

    当前makefile或shell内支持文件后缀的类型列表,意思是文件支持.cpp结尾的类型,并且将他,输出到OUTPUT_FILE函数. 见网上有人说: “makefile中 .SUFFIXES: . ...

  3. Linux启动管理:grub

    1.grub中分区表示 Linux 中 /dev/sda1   在grub中为   hd0,0    代表第一个硬盘的第一个分区 Linux中 /dev/sdb3是扩展分区     在grub中为   ...

  4. 结果集(result set)解释与用法

    解释: 引用自wiki: An SQL result set is a set of rows from a database, as well as metadata about the query ...

  5. WAMP启动失败简单解决方法

    一般情况下,直接选择安装,突然出现问题了:提示:msvcp110.dll或msvcr110.dll问题, 那么你直接复制这个来百度就行. 在百度会提示让你一键安装并且修复的. 或者你可能会看网上其他教 ...

  6. js DOM Element属性和方法整理

    节点操作,属性 1. childNodes.children 这两个属性获取到的子节点会根据浏览器的不同而不同的,所以一定要判断下nodeType是否为1. childNodes获取到的是NodeLi ...

  7. ZOJ 3711 Give Me Your Hand

    Give Me Your Hand Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge BellyWhite a ...

  8. Ubuntu 12 安装 搜狗输入法

    下载地址:http://pinyin.sogou.com/linux/?r=pinyin Ubuntu 12 中,安装搜狗输入法注意事项 http://pinyin.sogou.com/linux/h ...

  9. 自定义Listview

    public class MyListView extends ListView { public MyListView(Context context) { super(context); } pu ...

  10. css3延时动画

    不太理解属性都是什么意思,但是有动画效果,我也是惊呆了 <style> #animated_div{animation:animated_div 4s 1; -moz-animation: ...