<Trie> 212 <Array> 229
212. Word Search II
class TrieNode{
char val;
TrieNode[] children;
String word;
public TrieNode(char x){
children = new TrieNode[26];
word = null;
}
}
class Solution {
public List<String> findWords(char[][] board, String[] words) {
List<String> res = new ArrayList<>();
if(board == null || board.length == 0) return res;
TrieNode root = new TrieNode(' ');
buildTrie(root, words);
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
char c = board[i][j];
if(root.children[c - 'a'] != null){
dfs(board, i, j, root, res);
}
}
}
return res;
}
private void buildTrie(TrieNode root, String[] words){
for(String s : words){
TrieNode cur = root;
for(char c : s.toCharArray()){
if(cur.children[c - 'a'] == null){
cur.children[c - 'a'] = new TrieNode(c);
}
cur = cur.children[c - 'a'];
}
cur.word = s;
}
}
private void dfs(char[][] board, int i, int j, TrieNode cur, List<String> res){
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length) return;
char c = board[i][j];
if(c == '*') return;
if(cur.children[c - 'a'] == null) return;
cur = cur.children[c - 'a'];
if(cur.word != null){
res.add(cur.word);
cur.word = null;
}
board[i][j] = '*';
dfs(board, i + 1, j, cur, res);
dfs(board, i - 1, j, cur, res);
dfs(board, i, j + 1, cur, res);
dfs(board, i, j - 1, cur, res);
board[i][j] = c;
}
}
229. Majority Element II
Boyer-Moore Majority Vote algorithm
这道题让我们求出现次数大于 n/3 的数字,而且限定了时间和空间复杂度,那么就不能排序,也不能使用 HashMap,这么苛刻的限制条件只有一种方法能解了,那就是摩尔投票法 Moore Voting,这种方法在之前那道题Majority Element中也使用了。
- given n numbers and 1 counter (which is the majority element problem), at most (n/2) times pair-out can happen, which will lead to the survival of the only element that appeared more than n/2 times.
- given n numbers and 2 counters (which is our case), at most n/3 times of pair-out can happen, which will lead to the survival of elements that appeared more than n/3 times.
- given n numbers and k counters, at most (n/k+1) times of pair-out can happen, which will lead to the survival of elements that appeared more than n/(k+1) times.
当出现次数超过n/2的数的时候,只需要1个counter, 出现次数超过n/3的时候,需要2个counter,以此类推。
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<>();
if(nums.length == 0)
return res;
int num1 = nums[0]; int num2 = nums[0]; int count1 = 0; int count2 = 0 ;
for (int val : nums) {
if(val == num1)
count1++;
else if (val == num2)
count2++;
else if (count1 == 0) {
num1 = val;
count1++;
}
else if (count2 == 0) {
num2 = val;
count2++;
}
else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for(int val : nums) {
if(val == num1)
count1++;
else if(val == num2)
count2++;
}
if(count1 > nums.length/3)
res.add(num1);
if(count2 > nums.length/3)
res.add(num2);
return res;
}
}
<Trie> 212 <Array> 229的更多相关文章
- [leetcode trie]212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 双数组Trie树 (Double-array Trie) 及其应用
双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...
- loj517 计算几何瞎暴力(Trie树)
题目: https://loj.ac/problem/517 分析: 操作4比较特殊,我们先来分析下操作4 操作4相当于需要一个数据结构,使得里面的数据有序(这有很多选择) 结合操作1,操作4的“排序 ...
- python数据分析---第04章 NumPy基础:数组和矢量计算
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- php中文转拼音2
<?php /** * strtopinyin.php * * @name 汉字字符转拼音 * @author Kudosharry * @version v1.0 * */ class Str ...
- python数据分析系列(2)--numpy
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- python数据分析 Numpy基础 数组和矢量计算
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- bzoj2351 2462
我没写hash,写了一些奇怪的做法,好像被hash随便操了…… 如果没有多测,那么这道题是白书上的例题 把询问矩阵当作a个模板串,建成一个ac自动机 把一开始的矩阵当作n个串放到自动机上匹配,找到a个 ...
- bzoj1559
自动机上状压dp,把单词是否存在压成二进制位注意这里面某些单词会包含其他单词,所以某些自动机上有些状态点对应多个二进制位方案只要再顺着有方案的状态搜一遍即可 ..,'a'..'z'] of longi ...
随机推荐
- chentos7 安装jdk1.8(二进制安装)
首先下载jdk1.8 去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- 下载代码的时候 SSH与http的区别
SSH: git@gitlab.alibaba-inc.com:damai-mz/mz-market.git HTTP:http://gitlab.alibaba-inc.com/damai-mz/m ...
- 被“org.springframework.boot.web.support.SpringBootServletInitializer;”耽搁的两天
org.springframework.boot.web.support.SpringBootServletInitializer 改为: org.springframework.boot.conte ...
- java、JavaScript获取微信用户信息登录优化方案
1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...
- 超详细Pycharm部署项目视频教程
在实际的工作中,不管你是开发.测试还是运维人员,都应该掌握的一项技能就是部署项目,简单说就是把项目放到服务器中,使其正常运行.今天猪哥就以咱们的微信机器人项目为例子,带大家来部署一下项目.本文将会详细 ...
- QT+OpenGL(04)—freetype库的编译
1.freetype库的下载 https://www.freetype.org/download.html freetype-2.10.0.tar.bz2 2.解压 3.进入 freetype-2. ...
- Server2016 IIS安装配置
- Python - 数字 - 第六天
Python 数字(Number) Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象 ...
- 工作笔记--adb命令篇
1.抓log方法 (bat文件) mkdir D:\logcatset /p miaoshu=请描述操作:adb logcat -v threadtime > D:\logcat\%miaosh ...
- 用实力燃爆暑期丨i春秋渗透测试工程师线下就业班开课了!
i春秋&赛虎暑期渗透测试工程师线下就业班开课了! 本期开班地点:北京,面授脱产:四个月. 如果这次没来得及报名的同学也可以选择9月份广州的班次,具体开班时间请咨询谢老师:18513200565 ...