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中也使用了。

  1. 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.
  2. 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.
  3. 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的更多相关文章

  1. [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 ...

  2. 双数组Trie树 (Double-array Trie) 及其应用

    双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...

  3. loj517 计算几何瞎暴力(Trie树)

    题目: https://loj.ac/problem/517 分析: 操作4比较特殊,我们先来分析下操作4 操作4相当于需要一个数据结构,使得里面的数据有序(这有很多选择) 结合操作1,操作4的“排序 ...

  4. python数据分析---第04章 NumPy基础:数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  5. php中文转拼音2

    <?php /** * strtopinyin.php * * @name 汉字字符转拼音 * @author Kudosharry * @version v1.0 * */ class Str ...

  6. python数据分析系列(2)--numpy

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  7. python数据分析 Numpy基础 数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  8. bzoj2351 2462

    我没写hash,写了一些奇怪的做法,好像被hash随便操了…… 如果没有多测,那么这道题是白书上的例题 把询问矩阵当作a个模板串,建成一个ac自动机 把一开始的矩阵当作n个串放到自动机上匹配,找到a个 ...

  9. bzoj1559

    自动机上状压dp,把单词是否存在压成二进制位注意这里面某些单词会包含其他单词,所以某些自动机上有些状态点对应多个二进制位方案只要再顺着有方案的状态搜一遍即可 ..,'a'..'z'] of longi ...

随机推荐

  1. git pull --rebase的理解

    在使用git的过程中经常需要使用到git pull命令,在更新远端代码的同时如果与本地代码产生冲突了, 那么冲突的文件中就出现了需要手动合并的部分,而git pull --rebase不同的地方则是当 ...

  2. npm ERR! Cannot read property 'resolve' of undefined

    一 .有可能是版本过低,或者软件损坏,重新安装一下试试 地址

  3. 普通的行专列;oracle行专列;更新中。。。

    题记 本来想写一个完整的表创建,但是其他人都写过啦,要不这样,你们有什么行转列的问题给我留言,我直接回答如何 Oracle的行转列 这篇文章不错:https://blog.csdn.net/huay_ ...

  4. pixijs shader 制作百叶窗效果

    pixijs shader 制作百叶窗效果 直接贴代码了 const app = new PIXI.Application({ transparent: true }); document.body. ...

  5. Spring Security OAuth2 Demo —— 客户端模式(ClientCredentials)

    前情回顾 前几节分享了OAuth2的流程与其它三种授权模式,这几种授权模式复杂程度由大至小:授权码模式 > 隐式授权模式 > 密码模式 > 客户端模式 本文要讲的是最后一种也是最简单 ...

  6. Kubernetes 部署集群内部DNS服务

    Kubernetes 部署集群内部DNS服务 部署官网:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/ ...

  7. 2019-11-25-如何在国内发布-UWP-应用

    原文:2019-11-25-如何在国内发布-UWP-应用 title author date CreateTime categories 如何在国内发布 UWP 应用 lindexi 2019-11- ...

  8. C#进阶之路(八)集合的应用

    集合是我们编程时候常用的类库,本文主要讨论具体每个类型的区别,每个集合对应的时间复杂度.先上一个时间复杂度图: C#集体类型( Collections in C#) 集合是.NET FCL(Frame ...

  9. Spring面试题总结的很全面,附带超详细答案

    1.什么是Spring? Spring是一个开源的Java EE开发框架.Spring框架的核心功能可以应用在任何Java应用程序中,但对Java EE平台上的Web应用程序有更好的扩展性.Sprin ...

  10. JMeter之Http协议接口性能测试--基础

    一.不同角色眼中的接口 1.1,开发人员眼中的接口    1.2,测试人员眼中的接口 二.Http协议基本介绍 2.1,常见的接口协议 1.:2. :3. :4.:5.: 6. 2.2,Http协议栈 ...