滞在ボタン--- 2020年3月17日

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;
	}
}

より多くのあなたが知っている、より多くのあなたは知りません。
手術をせずに適切な方法は、患者はまだ手術を終了し、手術する方法はありません、求めることができます。
あなたが他の質問がある場合は、ウェルカムメッセージ、我々は、議論一緒に学び、一緒に進行することができます

彼は193元の記事を発表 ウォンの賞賛116 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_40722827/article/details/104931721