Leetcode_1679_K和数对的最大值_水题

12/12

有一说一,getordefault是真的慢
class Solution {
    
    
    public int maxOperations(int[] nums, int k) {
    
    
        Map<Integer,Integer>mp=new HashMap<>();
        int ans=0;
        for(int i:nums){
    
    
            int now=mp.getOrDefault(k-i,0);
            if(now!=0){
    
    
                ans++;
                mp.put(k-i,now-1);
            }else{
    
    
                mp.put(i,mp.getOrDefault(i,0)+1);
            }
        }
        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111058847