题目链接: Leetcode Weekly Contest 266
1、 Count Vowel Substrings of a String
难度:Easy
暴力求解,如果数据量大一点的话是一道难度不小的题。
代码:
class Solution {
public int countVowelSubstrings(String word) {
char[] s=word.toCharArray();
int[] count=new int[26];
for(int i=0;i<s.length;i++){
count[s[i]-'a']++;
}
if(count['a'-'a']==0||count['e'-'a']==0||count['i'-'a']==0||count['o'-'a']==0||count['u'-'a']==0){
return 0;
}
int n=s.length;
int res=0;
for(int i=0;i<n;i++){
Arrays.fill(count,0);
boolean flag=true;
for(int j=i;flag&&j<n;j++){
if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'){
count[s[j]-'a']++;
if(count['a'-'a']>0&&count['e'-'a']>0&&count['i'-'a']>0&&count['o'-'a']>0&&count['u'-'a']>0){
res++;
}
}
else{
flag=false;
}
}
}
return res;
}
}
2、Vowels of All Substrings
难度:Medium
思路:
参考高赞回答。
代码:
class Solution {
public long countVowels(String word) {
char[] s=word.toCharArray();
int n=s.length;
long res=0;
for(int i=0;i<n;i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){
res+=(i-0+1L)*(n-1-i+1L);//注意是1L,不然两数相乘可能超出int的范围哦
//(i-0+1)表示包含s[i]的substring开始位置有多少种选择,可以选择[0,i]
//(n-1-i+1)表示包含s[i]的substring开始位置有多少种选择,可以选择[i,n-1]
}
}
return res ;
}
}
3、2059. Minimum Operations to Convert Number
难度:Medium
思路:
求最大值的最小值,暴力会超时,用二分法。
代码
class Solution {
public int minimizedMaximum(int n, int[] q) {
Arrays.sort(q);
int l=1,r=q[q.length-1];
while(l<r){
int mid=l+(r-l)/2;
if(count(q,mid)<=n){
r=mid;
}
else{
l=mid+1;
}
}
return l;
}
public int count(int[] nums,int max){
//将nums[]中的元素按题意分配,分配的最大值为max,求至少需要几个位置
int res=0;
for(int n:nums){
if(n%max==0){
res+=(n/max);
}
else{
res+=(n/max+1);
}
}
return res;
}
}
4、 2065. Maximum Path Quality of a Graph
难度:Hard
思路
暂时不会。
代码
//