马蜂窝笔试编程题

晚了20分钟参加考试,心态炸裂。。。。编程题到最后也只做了第一题。不知道大家怎么做的。

我的如下:
package 马蜂窝;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeMap;


/**
 * 如果不存在记录,则修改城市数据
 * @author phion_yan
 *
 */
public class 统计 {

	static class Record{
		
		public Record(String city , int uid) {
			this.city = city;
			this.uid = uid;
		} @Override public boolean equals(Object obj) {
			
			return ((Record)obj).uid == this.uid&&
					((Record)obj).city.equals(this.city);
		} @Override public int hashCode() {
				return uid;
			}
		
		String city = "";
		int uid = 0;
	}
	
	
	static class City implements Comparable<City>{
		public City(String name,int users){
			this.name = name;
			this.users = users;
		}
		
		int users = 0;
		String name=""; @Override public int compareTo(City o) {
			if(o.users>this.users) {
				return 1;
			}else if(o.users<this.users) {
				return -1;
			}else {
				return this.name.compareTo(o.name);
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine();
		String[] strs = line.split(" ");
		
		Map<String,City> cityMap = new TreeMap<String,City>();
		HashMap<Record,Integer> recordMap = new HashMap<>();
		PriorityQueue<City> citys = new PriorityQueue<City>(new Comparator<City>() { @Override public int compare(City o1, City o2) {
				return o1.compareTo(o2);
			}
			
		});
		
		for(int i = 0 ; i< strs.length ; i++) {
			String[] data = strs[i].split("-");
			int uid = Integer.parseInt(data[0]);
			String city = data[1];
			Record r = new Record(city, uid);
			if(recordMap.containsKey(r)) {
				continue;
			}else {
				City c = null; 
				if(cityMap.containsKey(city)) {
					c = new City(city, cityMap.get(city).users+1);
				}else {
					c = new City(city, 1);
				}
				cityMap.put(city, c);
				recordMap.put(r, 1);
			}
		}
		Iterator i = cityMap.entrySet().iterator();
		while(i.hasNext()) {
			Map.Entry<String,City> entry = (Entry<String, City>) i.next();
			citys.add(entry.getValue());
		}
		for(int j = 0 ; j < 3 ; j++) {
			if(citys.isEmpty()) return;
			City c = citys.remove();
			System.out.println(c.name+" "+c.users);
		}
	}
	
	
}


#马蜂窝##笔试题目#
全部评论
后来想的写法,这样写比较简洁,用了些lambda表达式 import java.util.*; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String[] input = sc.nextLine().split(" ");         TreeMap<String, Set<String>> map = new TreeMap<>();         String[] mark;         Set<String> set;         for (String s : input) {             mark = s.split("-");             set = !map.containsKey(mark[1]) ? new HashSet<>() : map.get(mark[1]);             set.add(mark[0]);             map.put(mark[1], set);         }         List<Map.Entry<String, Set<String>>> list = new ArrayList<>(map.entrySet());         list.sort((Map.Entry<String, Set<String>> root, Map.Entry<String, Set<String>> next) ->                 next.getValue().size() - root.getValue().size() != 0 ? next.getValue().size() - root.getValue().size() : root.getKey().compareTo(next.getKey()));         for (int i = 0; i < 3; i++) {             System.out.println(list.get(i).getKey() + " " + list.get(i).getValue().size());         }     } }
点赞 回复
分享
发布于 2019-09-24 03:28
我也晚了20分钟,同样只A了第一题、、、
点赞 回复
分享
发布于 2019-09-23 20:38
联易融
校招火热招聘中
官网直投
这题需要这么麻烦莫😂
点赞 回复
分享
发布于 2019-09-23 20:40

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务