第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,下次争取前三题能在一个半小时内完成。加油加油。