本文主要是介绍第190场周赛,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第190场周赛
- 5416. 检查单词是否为句中其他单词的前缀
- 5417. 定长子串中元音的最大数目
- 5418. 二叉树中的伪回文路径
5416. 检查单词是否为句中其他单词的前缀
class Solution {public int isPrefixOfWord(String sentence, String searchWord) {String [] chars = sentence.split(" ");int len = chars.length;int compareLen = searchWord.length();int res = -1;for(int i=0;i<len;i++) {int curStrLen = chars[i].length();if(curStrLen<compareLen) {continue;}String firstStr = chars[i].substring(0, compareLen);if(firstStr.equals(searchWord)) {res = i+1;break;}}return res;}
}
5417. 定长子串中元音的最大数目
class Solution {public int maxVowels(String s, int k) {int len=s.length();if(len<k) {return 0;}int res = 0;int[] maxLen = new int[len+1];for(int i=1;i<=k;i++) {char cur = s.charAt(i-1);if(cur=='a' ||cur=='e' ||cur=='i'||cur=='o'||cur=='u') {maxLen[i]=maxLen[i-1]+1;}else {maxLen[i]=maxLen[i-1];}}res = res>maxLen[k]?res:maxLen[k];for(int i=k+1;i<=len;i++) {char cur = s.charAt(i-1);char former = s.charAt(i-1-k);boolean flag = false;if(former=='a' ||former=='e' ||former=='i'||former=='o'||former=='u') {flag = true;}else {flag = false;}if(cur=='a' ||cur=='e' ||cur=='i'||cur=='o'||cur=='u') {if(flag) {maxLen[i]=maxLen[i-1];}else {maxLen[i]=maxLen[i-1]+1;}}else {if(flag) {maxLen[i]=maxLen[i-1]-1;}else {maxLen[i]=maxLen[i-1];}}res = res>maxLen[i]?res:maxLen[i];}return res;}
}
5418. 二叉树中的伪回文路径
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int pseudoPalindromicPaths (TreeNode root) {if(root==null) {return 0;}if(root.left==null && root.right==null) {return 1;}Set<Integer> set = new HashSet<Integer>();//set.add(root.val);int res = maxPaths(root,set);return res;}public int maxPaths (TreeNode root,Set<Integer> set) {
// System.out.println("curr node:"+root.val);boolean flag = true;if(set.contains(root.val)) {flag = false;set.remove(root.val);}else {flag = true;set.add(root.val);}
// for(Integer s:set) {
// System.out.print(s+" ");
// }
// System.out.println();if(root.left==null && root.right==null) {//判断是否是伪回文int size = 0;if(set.size()<=1) { size = 1;}if(!flag) {set.add(root.val);}else{set.remove(root.val);}return size;}int left = 0;if(root.left!=null) {left = maxPaths(root.left,set);}int right = 0;if(root.right!=null) {right = maxPaths(root.right,set);}if(!flag) {set.add(root.val);}else{set.remove(root.val);}return left+right;}
}
这次首次在一个半小时内完成三道题,但是排名还是不高
目标,以后稳定完成3道题的前提下,冲进1K内
这篇关于第190场周赛的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!