第194场周赛

第194场周赛

  • 1486. 数组异或操作
  • 1487. 保证文件名唯一
  • 1488. 避免洪水泛滥

1486. 数组异或操作

class Solution {
    public int xorOperation(int n, int start) {
        int res = start;
        for(int i=1;i<n;i++){
            start = start+2;
            res^=start;
        }
        return res;
    }
}

1487. 保证文件名唯一

class Solution {
    public String[] getFolderNames(String[] names) {
            	if(names == null || names.length==0){
    		return null;
    	}
    	int len = names.length;
    	String[] res = new String[len];
    	Map<String,Integer> map = new HashMap<String,Integer>();
    	for(int i=0;i<len;i++){
    
    		
    		
    		//不包含
    		if(!map.containsKey(names[i])){
    			res[i] = names[i];
    			map.put(names[i],1);
    		}else{
                int count=map.get(names[i]);
    			while (map.containsKey(names[i] + "(" + count + ")")) {
                    count++;
                }
                
                map.put(names[i] + "(" + count + ")", 1);
                map.put(names[i], map.get(names[i])+1);
                // 本次的结果
                res[i] = names[i] + "(" + count + ")";

    					
    		}
    	}
    	return res;
    }
}

1488. 避免洪水泛滥

class Solution {
    public int[] avoidFlood(int[] rains) {
       int len = rains.length;
    	int[] res = new int[len];
    	
    	Map<Integer,Integer> water = new HashMap<Integer,Integer>();
    	TreeSet<Integer> zero = new TreeSet<Integer>();
    	
    	for(int i=0;i<len;i++){
    		int r = rains[i];
    		//System.out.println("r:"+r);
    		if(r==0){
    			res[i]=1;
    			zero.add(i);
    			continue;
    		}
    		
    		if(water.containsKey(r)){
    			int preIdx = water.get(r);
    			Integer zeroIdx = zero.higher(preIdx);
    			if(zeroIdx==null){
    				return new int[0];
    			}
    			zero.remove(zeroIdx);
    			res[zeroIdx] = r;
    			
    		}
    		water.put(r,i);
    		res[i]= -1;
    	}
    	return res;
    }
}

你可能感兴趣的:(LeetCode)