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 ...
随机推荐
- 网络广告术语CPC、CPM和CTR的含义和关系
1. CPC(Cost-per-click):对于广告主来说,就是每次点击(广告)的(付给网站主的)成本:对于媒体(或网站主)来说,就是用户每次点击(广告)(向广告主收取)的费用.可以用公 ...
- centos 7.0 查看根目录下所有文件夹
centos 7.0最小化安装 第一行是登录 [root@localhost ~]# [root@localhost ~]# cd ../ [root@localhost /]# ls bin dev ...
- AWK命令的用法
1.awk命令简介: awk是一种可以处理数据.产生格式化报表的语言,功能十分强大. awk的工作方式是读取数据,将每一行数据视为一条记录(record)每笔记录以字段分隔符分成若干字段,然后输出各个 ...
- 在VS2013中强制IIS Express应用程序池使用经典模式
直接在文件夹地址栏输入 %userprofile%\documents\iisexpress\config\applicationhost.config 会打开上边的配置文件 将 <siteDe ...
- 本地wampserver如何配置伪静态
本地wamp实现虚拟主机后,我把自己的站放进去就出现了500错误看日志看到.htaccess: Invalid command ‘RewriteEngine’, perhaps misspelled ...
- 3步完成chrome切换搜索引擎
1.打开chrome://settings/,找到搜索 2.点击“管理搜索引擎”,出现弹窗. 增加搜索引擎,三个文本框分别输入:名称.快捷键.地址 3.在新的选项卡中,输入快捷键(如:github), ...
- Markdown语言详解
相信大家在github上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的README.md文件,也不曾对这个文件有过什么了解.但是在看到别人写的项目的README.md里面竟然有图 ...
- CentOS 与 RedHat 关系和区别
转自http://www.aixchina.net/club/archiver/tid-26784.html CentOS 发行版介绍 CentOS 是 Community ENTerprise Op ...
- CF469D Two Set (并查集)
Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per te ...
- CF464A (模拟)
http://codeforces.com/contest/465/problem/C Codeforces Round #265 (Div. 2) C Codeforces Round #265 ( ...