首页 > 其他 > 详细

LeetCode 第 150 场周赛

时间:2019-08-19 23:51:36      阅读:142      评论:0      收藏:0      [点我收藏+]

public int countCharacters(String[] words, String chars) {
        char[] array = chars.toCharArray();
        List<String> charList = new ArrayList<>();
        List<String> result = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            charList.add(String.valueOf(array[i]));
        }
        for (int i = 0; i < words.length; i++) {
            if (isRight(words[i], charList)) {
                result.add(words[i]);
            }
        }
        int res = 0;
        for (String s : result) {
            res += s.length();
        }
        return res;
    }


    private boolean isRight(String str, List<String> charList) {
        List<String> tempList = new ArrayList<>();
        tempList.addAll(charList);
        char[] array = str.toCharArray();
        boolean flag = false;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < tempList.size(); j++) {
                if (tempList.get(j).equals(String.valueOf(array[i]))) {
                    tempList.remove(j);
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return false;
            }
            flag = false;
        }
        return true;
    }
public int maxLevelSum(TreeNode root) {
        if (root == null) {
            throw new IllegalArgumentException("invalid parameters");
        }

        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        int index = 0;
        int sum = 0;
        //当队列不是空的时候,将队列头移除,将其儿子节点加入队列
        int ceng = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();//记录当前层节点的个数
            TreeNode temp;
            int result = 0;
            //遍历size次
            for (int i = 0; i < size; i++) {
                temp = queue.poll();
                result += temp.val;
                if (temp.left != null) {
                    queue.offer(temp.left);
                }
                if (temp.right!= null) {
                    queue.offer(temp.right);
                }
            }
            if (result > sum) {
                    index = ceng;
                    sum = result;
                }
            ceng++;
        }
        return index;
    }

LeetCode 第 150 场周赛

原文:https://www.cnblogs.com/fonxian/p/11380305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!