Buzz words
给你一个字符串和字典,从头扫到位,如果到当前的字符的字符串存在于字典中,则显示 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的更多相关文章
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- 412. Fizz Buzz
https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...
- Lintcode 9.Fizz Buzz 问题
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode之412. Fizz Buzz
-------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public cl ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式
写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...
随机推荐
- target 事件属性
定义和用法 target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素.文档或窗口. 语法 event.target 实例 下面的例子可获得触发事件的元素: <html&g ...
- echarts X轴数据显示不全问题
很奇怪,X轴只显示了部分节点.没有显示全. 在xAxis上加上下面的配置就能解决: xAxis: [ { type: 'category', axisLabel :{ interval:0 }, // ...
- 遍历JObject
JObject jo = JObject.Parse(jsonStr); IEnumerable<JProperty> properties = jo.Properties(); fore ...
- JSF的CommandXxx组件的update用法总结
Update all forms 1 update="@(form)" Update first form 1 update="@(form:first)" U ...
- jquery 获取和设置 checkbox radio 和 select option的值?
============== 获取和设置 checkbox radio 和 select的值? === val()函数, 其名字就表达了 它的意思: 他就是= value 的简写! val就是valu ...
- nginx下面server配置
haomeiv配置 log_format www.haomeiv.com '$remote_addr - $remote_user [$time_local] "$request" ...
- vijos1740 聪明的质监员 (二分、区间求和)
http://www.rqnoj.cn/problem/657 https://www.vijos.org/p/1740 P1740聪明的质检员 请登录后递交 标签:NOIP提高组2011[显示标签] ...
- Mastering Web Application Development with AngularJS 读书笔记(二)
第一章笔记 (二) 一.scopes的层级和事件系统(the eventing system) 在层级中管理的scopes可以被用做事件总线.AngularJS 允许我们去传播已经命名的事件用一种有效 ...
- C# 绘制统计图(柱状图, 折线图, 扇形图)【转载】
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...
- jquery Ajax跨域调用WebServices方法
由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...