第185场周赛

文章目录

  • 第185场周赛
    • 5388. 重新格式化字符串
    • 5389. 点菜展示表
    • 5390. 数青蛙

第185场周赛

5388. 重新格式化字符串

class Solution {
    public String reformat(String s) {
    	if(s==null || s.length()==0) {
    		return "";
    	}
    	int len = s.length();
    	List str = new ArrayList();
    	List nums = new ArrayList();
    	int strCount =0;
    	int numCount=0;
    	for(int i=0;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

5389. 点菜展示表

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

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

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

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o1.compareTo(o2);
			}
    		
    	});
    	
    	//按照排序排序的菜名添加到输出
    	List> showMenus = new ArrayList<>();
    	for(Map.Entry> entry:tableFoodMap.entrySet()) {
    		String TableId = entry.getKey();
    		List 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

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

你可能感兴趣的:(LeetCode)