Leetcode Weekly Contest 266(数学、二分)

题目链接: 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

思路

暂时不会。

代码

//
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值