美团 8-22 编程题 后台开发

第一题 AC 100%
package LinkCode;

import java.util.*;

public class meituan1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		List<Integer> res = new ArrayList<Integer>();
		res = cal(str);
		for (Integer temp : res)
			System.out.print(temp + " ");
		sc.close();
	}

	public static List<Integer> cal(String str) {
		// TODO Auto-generated method stub
		int[] arr = new int[128];
		for (int i = 0; i < str.length(); i++)
			arr[str.charAt(i)] = i;
		List<Integer> list = new ArrayList<>();
		int low = 0, high = 0;
		for (int i = 0; i < str.length(); i++) {
			high = Math.max(high, arr[str.charAt(i)]);
			if (i == high) {
				list.add(high - low + 1);
				low = high + 1;
			}
		}
		return list;
	}
}

第二题  AC 73%
package LinkCode;

import java.util.*;

public class meituan2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String str = sc.nextLine().trim().toLowerCase();
			String[] words = str.split(" ");
			System.out.println(cal(words));
		}
		sc.close();
	}

	public static String cal(String[] words) {
		// TODO Auto-generated method stub
		Map<Character, Set<Character>> map1 = new HashMap<>();
		Map<Character, Integer> map2 = new HashMap<>();
		String key = "invalid";
		String dig = "0123456789";
		for (int i = 0; i < words.length; i++) {
			if (words[i].contains(dig))
				return key;
		}
		for (String word : words) {
			for (char c : word.toCharArray()) {
				if (map1.containsKey(c)) {
					continue;
				}
				map1.put(c, new HashSet<>());
				map2.put(c, 0);
			}
		}
		for (int i = 0; i < words.length - 1; ++i) {
			String temp = words[i];
			String str = words[i + 1];
			for (int j = 0; j < Math.min(temp.length(), str.length()); ++j) {
				char x = temp.charAt(j);
				char y = str.charAt(j);
				if (x != y) {
					if (map1.get(x).add(y)) {
						map2.put(y, map2.get(y) + 1);
					}
					break;
				}
			}
		}

		StringBuilder sb = new StringBuilder();
		Queue<Character> list = new LinkedList<>();
		for (Map.Entry<Character, Integer> entry : map2.entrySet()) {
			if (entry.getValue() == 0) {
				list.offer(entry.getKey());
			}
		}
		while (!list.isEmpty()) {
			char c = list.poll();
			sb.append(c);
			for (char ch : map1.get(c)) {
				map2.put(ch, map2.get(ch) - 1);
				if (map2.get(ch) == 0) {
					list.offer(ch);
				}
			}
		}
		return sb.length() < map2.size() ? key : sb.toString();
	}
}


#Java##美团#
全部评论
哪个方向?
点赞 回复
分享
发布于 2019-08-22 17:05
和我一样,第一题AC,第二题73.不过我问答题不会,估计凉凉了。。。
点赞 回复
分享
发布于 2019-08-22 17:06
滴滴
校招火热招聘中
官网直投
没做笔试,一共多少题?
点赞 回复
分享
发布于 2019-08-22 17:16
问答题你会吗,,怎么解释?
点赞 回复
分享
发布于 2019-08-22 17:23
小姐姐可以说一下第一题吗 第一题没看懂题系列。。。。
点赞 回复
分享
发布于 2019-08-22 17:26
有人看懂20分的问答题了吗?我直接空了没写,点开,看完,关闭。还有选择题,有些名词也不认识,留下了属于弱者的眼泪。😂
点赞 回复
分享
发布于 2019-08-22 18:49
大佬,菜鸡表示没看懂第一题思路,能否贴个题解😥😥
点赞 回复
分享
发布于 2019-08-23 19:51

相关推荐

2 12 评论
分享
牛客网
牛客企业服务