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;
}
原文:https://www.cnblogs.com/fonxian/p/11380305.html