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 ...
随机推荐
- AutoMocker单元测试
/// <summary> /// 测试获取所有物流 /// </summary> [TestMethod] public void TestExpressController ...
- Struts2版本配置2.1以前与以后---关于filter的配置
严重: Dispatcher initialization failed java.lang.RuntimeException JavaStrutsfreemarkerApacheXML 严重: D ...
- 第一篇随笔!!!THE FIRST BLOOD!!!
注册成功,哈哈 以后就要在这扎窝了,要坚持记录下去,关于生活,关于学习. 选了个皮肤 darkgreen,不错,简洁舒服,喜欢. 好,差不多了,感觉没什么可说的了...==
- Owin是什么?
OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平台的开放Web接口. 那Web接口是谁和谁之间的接口呢?是 ...
- SqlServer 18456 错误解决方案
sqlserver 安装需注意事项: 不要选择,否则运行一段时间后sqlserver登录时会出现18456错误. 发生18456错误(sa 可登录的情况下)解决本地用户登录问题的方案: <1& ...
- Linux 脚本 sh 和 ./ 的区别
如果.不在PATH里面,要执行当前目录下的可执行文件,使用全路径:./executable-file PATH是环境变量,如果将当前目录“./”添加到环境变量中,那么也可以不用“./”,直接输入当前目 ...
- [Json.net]忽略不需要的字段
摘要 在序列化对象,总会遇到一些敏感的信息,这些信息,并不想对调用接口的用户暴露出来,又或者移动端调用接口的时候,为了不返回没用的信息占用流量,这个时候也需要把一些信息给过滤掉. 系列文章 [Json ...
- 如何才能实现在点击链接时直接在网页中打开word文档,但不提示保存
一般要直接打开需要客户端 1.客户端有word支持 2.客户端浏览器的版本与设置 可寻找一下相关的控件或中间件,我的意见是看能否变通一下,把word转成HTML或PDF再展示给用户.(若用户不需要编辑 ...
- nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后,重新加 ...
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...