<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 ...
随机推荐
- IPv6地址编址
- 基于Django的Rest Framework框架的频率组件
0|1一.频率组件的作用 在我们平常浏览网站的时候会发现,一个功能你点击很多次后,系统会让你休息会在点击,这其实就是频率控制,主要作用是限制你在一定时间内提交请求的次数,减少服务器的压力. modle ...
- JWT签名与验签
签名Token生产 using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; usi ...
- 自动化API之一 自动生成Mysql数据库的微服务API
本文演示如何利用Uniconnector平台,自动生成Mysql数据库的API,节约开发人员编写后台API的时间.使用生成API的前提是开发者有 自己的数据库,有数据库的管理权限,并能通过外网 ...
- 转载-logbock.xml
转载:CS408 Spring boot——logback 基础使用篇:https://www.cnblogs.com/lixuwu/p/5804793.html#autoid-0-0-0 阅读目录 ...
- Delta-wave HDU - 1030
Delta-wave HDU - 1030 A triangle field is numbered with successive integers in the way shown on the ...
- SqlHelper发布——比你期望的还要多的多(例如比MyBatis-Pagehelper性能更高)
SqlHelper发布——比Mybatis-PageHelper性能更高 起源 前段时间开启了一个新的项目,在选择分页插件时,发现github上很流行的一个是pagehelper,在百度上搜索了一下, ...
- 【LOJ#3145】[APIO2019]桥梁(分块,并查集)
[LOJ#3145][APIO2019]桥梁(分块,并查集) 题面 LOJ 题解 因为某个\(\text{subtask}\)没判\(n=1\)的情况导致我自闭了很久的题目... 如果没有修改操作,可 ...
- 打开centos7图形化窗口
1. yum groupinstall "X Window System" 2. export DISPLAY=172.16.4.240:0.0 3. yum -y install ...
- java基于NIO的分散读取文件,然后统一聚合后写入文件
分散读取:对于一个文件,可以分散的读取数据,可以快速的读取,好比多个线程在分段同时读取: 聚合写入:为了提高效率,一般读取到的数据都是分散的,要快速写入,就需要把分散的数据聚集在一起,然后一块写入到文 ...