第185场周赛

第185场周赛

5388. 重新格式化字符串

class Solution {
    public String reformat(String s) {
    	if(s==null || s.length()==0) {
    		return "";
    	}
    	int len = s.length();
    	List<Character> str = new ArrayList<Character>();
    	List<Character> nums = new ArrayList<Character>();
    	int strCount =0;
    	int numCount=0;
    	for(int i=0;i<len;i++) {
    		if(s.charAt(i)>='a' && s.charAt(i)<='z') {
    			str.add(s.charAt(i));
    			strCount++;
    		}else if(s.charAt(i)>='0'&& s.charAt(i)<='9') {
    			nums.add(s.charAt(i));
    			numCount++;
    		}else {
    			return "";
    		}
    	}
    	
    	StringBuilder res = new StringBuilder();
    	if(Math.abs(strCount-numCount)>1) {
    		return "";
    	}else {
    		if(strCount>numCount) {
    			res.append(str.remove(0));
    			while(!nums.isEmpty()) {
        			res.append(nums.remove(0));
        			res.append(str.remove(0));
        		}
    		}else if (strCount<numCount){
    			res.append(nums.remove(0));
    			while(!str.isEmpty()) {
        			res.append(str.remove(0));
        			res.append(nums.remove(0));
        		}
    		}else {
    			while(!str.isEmpty()) {
        			res.append(str.remove(0));
        			res.append(nums.remove(0));
        		}
    		}
    		
    	}
    	
    	return res.toString();
    }
}

5389. 点菜展示表

参考了别人的代码,这题比赛的时候没写出来,参考链接

class Solution {
    public List<List<String>> displayTable(List<List<String>> orders) {
//存table和点菜时就按照table排序 ***这个写法以前没用过,牢记,实用
    	//用来存放tale和点菜的对应关系
    	TreeMap<String,Map<String,Integer>> tableFoodMap = new TreeMap(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return Integer.parseInt(o1)-Integer.parseInt(o2);
			}
    		
    	});
    	
    	//因为事前不确定有哪些食物,所以要用foodNameSet保存所有出现过的菜名
    	Set<String> foodNameSet = new HashSet<>();
    	for(List<String> order:orders) {
    		String TableId = order.get(1);
    		String foodName = order.get(2);
    		
    		foodNameSet.add(foodName);
    		
    		if(!tableFoodMap.containsKey(TableId)) {
    			Map<String,Integer> foodCountMap = new HashMap<>();
    			foodCountMap.put(foodName,1);
    			tableFoodMap.put(TableId, foodCountMap);
    		}else {
    			Map<String,Integer> foodCountMap = tableFoodMap.get(TableId);
    			foodCountMap.put(foodName, foodCountMap.getOrDefault(foodName,0)+1);
    			tableFoodMap.put(TableId, foodCountMap);
    		}
    	}
    	
    	//菜名按字母排序
    	List<String> orderedFoodNames = new ArrayList<>(foodNameSet);
    	orderedFoodNames.sort(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o1.compareTo(o2);
			}
    		
    	});
    	
    	//按照排序排序的菜名添加到输出
    	List<List<String>> showMenus = new ArrayList<>();
    	for(Map.Entry<String, Map<String,Integer>> entry:tableFoodMap.entrySet()) {
    		String TableId = entry.getKey();
    		List<String> showMenu = new ArrayList<>();
    		showMenu.add(TableId);
    		for(String foodName:orderedFoodNames) {
    			showMenu.add(entry.getValue().getOrDefault(foodName,0)+"");//+""是为了将Int转换成String
    		}
    		showMenus.add(showMenu);
    	}
    	
    	//加标题
    	orderedFoodNames.add(0, "Table");
    	showMenus.add(0, orderedFoodNames);
    	return showMenus;
    }
}

5390. 数青蛙

class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
int len = croakOfFrogs.length();
    	if(len%5!=0 || len==0) {
    		return -1;
    	}
    	int max = len/5;
    	int [] nums = new int[max];
//    	for(int i=0;i<max;i++) {
//    		nums[i]=Integer.MIN_VALUE;
//    	}
    	int res=-1;
    	char[] croak = {'c','r','o','a','k'};
    	for(int i=0;i<len;i++) {

    		char cur =croakOfFrogs.charAt(i);
//    		System.out.print(cur+" ");
    		int j=Integer.MIN_VALUE;
    		switch(cur){
    			case 'c':
    				j=0;
    				break;
    			case 'r':
    				j=1;
    				break;
    			case 'o':
    				j=2;
    				break;
    			case 'a':
    				j=3;
    				break;
    			case 'k':
    				j=4;
    				break;
    		}
    		boolean find = false;	
    		for(int k=0;k<max;k++) {
    			if(nums[k]==j) {
//    				System.out.println(k+" "+nums[k]);
    				find = true;
    				nums[k]++;
    				if(nums[k]==5) {
    					nums[k]=0;
    					res=Math.max(k+1, res);
    				}
    				break;
    			}
    		}
    		
    		if(find==false) {
    			return -1;
    		}
    		

    	}
    	return res;
    }
}

LONG WAY TO GO,下次争取前三题能在一个半小时内完成。加油加油。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值