1160. 拼写单词
class Solution {
public int countCharacters(String[] words, String chars) {
int[] chars_count = count(chars); // 统计字母表的字母出现次数
int res = 0;
for (String word : words) {
int[] word_count = count(word); // 统计单词的字母出现次数
if (contains(chars_count, word_count)) {
res += word.length();
}
}
return res;
}
// 检查字母表的字母出现次数是否覆盖单词的字母出现次数
boolean contains(int[] chars_count, int[] word_count) {
for (int i = 0; i < 26; i++) {
if (chars_count[i] < word_count[i]) {
return false;
}
}
return true;
}
// 统计 26 个字母出现的次数
int[] count(String word) {
int[] counter = new int[26];
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
counter[c-'a']++;
}
return counter;
}
}
class Solution {
public int countCharacters(String[] words, String chars) {
int[] hash = new int[26];
for(char ch : chars.toCharArray()){
hash[ch - 'a'] += 1;
}
int[] map = new int[26];
int len = 0;
for(String word : words){
Arrays.fill(map, 0);
boolean flag = true;
for(char ch : word.toCharArray()){
map[ch - 'a']++;
if(map[ch - 'a'] > hash[ch - 'a']) flag = false;
}
len += flag ? word.length() : 0;
}
return len;
}
}
面试题55 - I. 二叉树的深度
//递归
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftRoot = maxDepth(root.left);
int rightRoot = maxDepth(root.right);
int max = Math.max(leftRoot,rightRoot)+1;
return max;
}
}
//非递归 后序遍历
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode lastVisit = root;
TreeNode top;
int res = 0;
while (!stack.isEmpty()) {
res = Math.max(res, stack.size());
top = stack.peek();
if (top.left != null && lastVisit != top.left && lastVisit != top.right) {
stack.push(top.left);
} else if (top.right != null && lastVisit != top.right) {
stack.push(top.right);
} else {
lastVisit = stack.pop();
}
}
return res;
}
}
//非递归 层次遍历
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
TreeNode levelLast = root;
TreeNode visit;
int depth = 0;
while(!queue.isEmpty()) {
visit = queue.poll();
if (visit.left != null) {
queue.offer(visit.left);
}
if (visit.right != null) {
queue.offer(visit.right);
}
if (visit == levelLast) {
levelLast = queue.peekLast();
++depth;
}
}
return depth;
}
}
面试题27. 二叉树的镜像
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null) return root;
TreeNode temp = mirrorTree(root.left);
root.left = mirrorTree(root.right);
root.right = temp;
return root;
}
}
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root==null) {
return null;
}
//将二叉树中的节点逐层放入队列中,再迭代处理队列中的元素
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()) {
//每次都从队列中拿一个节点,并交换这个节点的左右子树
TreeNode tmp = queue.poll();
TreeNode left = tmp.left;
tmp.left = tmp.right;
tmp.right = left;
//如果当前节点的左子树不为空,则放入队列等待后续处理
if(tmp.left!=null) {
queue.add(tmp.left);
}
//如果当前节点的右子树不为空,则放入队列等待后续处理
if(tmp.right!=null) {
queue.add(tmp.right);
}
}
//返回处理完的根节点
return root;
}
}
你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步