摘要:
统计连续出现的 0 和 1 出现的次数,保存到一个counts数组中,如00110110,对应的数组为{2,2,1,2,1},所能组成子串的个数为min{u,v},u和v分别代表0和1的个数或者1和0的个数。应该返回2+1+1+1=5 法1 class Solution { public int c 阅读全文
摘要:
这个层序遍历要求返回每层的节点,正常的BFS从队列中弹出一个节点后就判断其有没有左子树和右子树,所以直接用BFS实现的话无法分层输出。 需要记录每层的节点数目,增加一个for循环就可以了。 /** * Definition for a binary tree node. * public class 阅读全文
摘要:
可以想到,数组中会出现“断层”,直接遍历一次即可。不存在【1,2,3,4,5】旋转成【5,4,3,2,1】的情况。 暴力法(我感觉还行啊,为什么被叫暴力): class Solution { public int minArray(int[] numbers) { int n = numbers.l 阅读全文
摘要:
1.层序遍历,一个队列存放节点,一个队列存放到当前节点的值。 2.递归 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * 阅读全文
摘要:
法官的入度为N-1,出度为0。 class Solution { public int findJudge(int N, int[][] trust) { int indegree[] = new int[N+1]; int outdegree[] = new int[N+1]; int resul 阅读全文
摘要:
就是栈。用map存储括号类型,遇到右括号弹栈,不匹配为false。左括号压栈。 class Solution { HashMap<Character,Character> chars; public Solution(){ this.chars = new HashMap<Character,Cha 阅读全文
摘要:
非递减就是升序,只不过有些元素是重复的。 根据样例,上下对比一下,可以想到先排好序,然后一个一个对比,不一样的就是需要移动的。不是求移动步数,是求需要移动的元素个数。 桶排序,遍历所有桶,直到所有桶中都没有元素。 class Solution { public int heightChecker(i 阅读全文