<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 ...
随机推荐
- JDBC数据库连接测试工具
贴代码 import java.io.PrintStream; import java.sql.*; import java.util.Properties; public class ZJdbcPi ...
- 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
本文由原作者松若章原创发布,作者主页:zhihu.com/people/hrsonion/posts,感谢原作者的无私分享. 1.引言 一道经典的面试题是:从 URL 在浏览器被被输入到页面展现的过程 ...
- css不常用的4个选择器-个人向
①:element1.element2(给同时满足有element1和element2 2个类名的元素添加样式) <!DOCTYPE html> <html> <head ...
- matplotlib的使用——pie(饼图)的使用
在我们进行数据分析的时候需要对得出的数据进行可视化,因此我们需要引入第三方包来帮助我们进行可视化分析,在这里使用matplotlib 一.安装 使用指令[pip install matplotlib] ...
- powershell与linux bash对比
转自Github/Powershell Bash PowerShell Description ls dir, Get-ChildItem List files and folders tree di ...
- DateTimeComparer
public int Compare(string x,string y) { DateTime xDate = DateTime.ParseExact(x, "MMMM", ne ...
- JDK1.8新特性——Stream API
JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...
- Vue笔记3
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ext.create使用(下)
本文介绍第三种使用方法: //通过类的引用实例化一个类 var w1 = Ext.create(Ext.window.Window, {//类的引用 title: '窗体', html:'<fo ...
- SAP 如何得到交货单上的序列号清单?
SAP 如何得到交货单上的序列号清单? 以内向交货单为例(外向交货单方法了类似)予以说明. 1)VL33N,在交货单显示界面, 但是没办法通过这个界面里导出序列号清单. 2),只能通过查表的方式导出序 ...