第168场周赛

5291. 统计位数为偶数的数字

class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        for(int i=0;i<nums.length;i++){
            int curr = nums[i];
            int temp = 0;
            while(curr>0){
                curr = curr/10;
                temp++;
            }
            if(temp>0 && temp%2==0){
                count++;
            }
        }
        return count;
    } 
}

5292. 划分数组为连续数字的集合

class Solution {
    public boolean isPossibleDivide(int[] nums, int k) {
        if(nums.length <=0 || nums.length%k !=0){
            return false;
        }
        Arrays.sort(nums);
        /*
        for(int i=0;i<nums.length;i++){
            System.out.print(nums[i]+" ");
        }
        */
        List<Integer> arrayList = new ArrayList<>(nums.length);
        for (int i=0;i<nums.length;i++) {
            arrayList.add(nums[i]);
        }
        while(arrayList.size()!=0){
            Integer first = arrayList.get(0);
            int index = 0;
            int count=0;
            while(count<k){
                if(arrayList.get(index)==(first+count)){
                    arrayList.remove(index);
                    count++;
                }else if(arrayList.get(index)<(first+count)){
                    index++;
                }else{
                    return false;
                }
            }
        }
        return true;
        
    }
}

5293. 子串的最大出现次数

class Solution {
    public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
        //System.out.println(s);
        //System.out.println("s.length()="+s.length());
        int i = 0;
        int j = s.length()-1;
        while((!(s.charAt(i)>='a' && s.charAt(i)<='z')) && !(s.charAt(i)>='A' && s.charAt(i)<='Z')){
            i++;
        }
        while((!(s.charAt(j)>='a' && s.charAt(j)<='z')) && !(s.charAt(j)>='A' && s.charAt(j)<='Z')){
            j--;
        }
        s = s.substring(i,j+1);
        //System.out.println("currStr:"+s);
        int length = s.length();
        Map<String,Integer> maps = new HashMap<String,Integer>();
        for(int left = 0;left<=length-minSize;left++){
            String substr = s.substring(left,left+minSize);
            if(maps.containsKey(substr)){
                
                maps.put(substr,maps.get(substr)+1);
            }else{
                if(verify(substr,maxLetters)){
                    //System.out.println(substr+" ok");
                    maps.put(substr,1);
                }
            }
        }
        int res = 0;
        if(maps.size()==0 || maps==null){
            return 0;
        }
        for(String str: maps.keySet()){
            int value = maps.get(str);
            if(res<value){
                res = value;
            }
        }
        //System.out.println("res="+res);
        return res;
    }
    public boolean verify(String substr,int maxLetters){
        Set<Character> set = new HashSet<Character>();
        for(int i=0;i<substr.length();i++){
            if(!set.contains(substr.charAt(i))){
                set.add(substr.charAt(i));
            }
            if(set.size()>maxLetters){
                return false;
            }
        }
        return true;
    }
}

1298. 你能从盒子里获得的最大糖果数

这题不会写,看了别人的题解写的,这个思路很清晰。

class Solution {
    public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
        int length = status.length;
        //访问过的盒子
        boolean[] visited = new boolean[length];
        //拥有的盒子
        Set<Integer> haveBoxes = new HashSet<Integer>();
        //拥有的钥匙
        Set<Integer> haveKeys = new HashSet<Integer>();
        Queue<Integer> q = new LinkedList<Integer>();
        for(int i=0;i<initialBoxes.length;i++){
            int index = initialBoxes[i];
            haveBoxes.add(index);
            if(status[index]==1){
                q.offer(index);
                visited[index]=true;
            }
        }
        int count = 0;
        while(!q.isEmpty()){
            Integer cur = q.poll();
            count+=candies[cur];
            int[] currKeys = keys[cur];
            int[] currBoxes = containedBoxes[cur];
            //遍历钥匙
            for(int key:currKeys){
                haveKeys.add(key);
                // 盒子没被拆开过 且 我们有对应的钥匙
                if(!visited[key] && haveBoxes.contains(key)){
                    q.offer(key);
                    visited[key]=true;
                }
            }

            //遍历盒子
            for(int box:currBoxes){
                haveBoxes.add(box);
                // 盒子没被拆开过 且 (我们有对应的钥匙 或者 盒子是打开状态)
                if(!visited[box] && (haveKeys.contains(box) || status[box]==1)){
                    q.offer(box);
                    visited[box]=true;
                }
            }
        }
        return count;
    }
}
发布了21 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Tracy_Yuan2014/article/details/103656001
今日推荐