3.12美团笔试Java代码

还是太菜,没有一道AC。
  1. 第一道90%
  2. 第二道90%
  3. 第三道看错题了(代码是错的,最后仔细看题才发现,没时间改了)过了36%
  4. 第四题读题7分钟没写
  5. 第五题动了动手,发现看不懂题,打扰了。。。
代码写的有点丑。。。
第一题dp:
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		char[][] map = new char[2][n];
		for (int row = 0;row < 2;row++) {		
			map[row]= input.next().toCharArray();
		}
		if (n == 1) {
			if (map[0][0] == '.' && map[1][0] == '.')
				System.out.println(1);
			else
				System.out.println(-1);
		}  else {
			int cnt = 0;
			boolean flag = true;
			for (int col = n - 1;col >= 1;col--) {
				if (map[0][col] == '.' && map[1][col] == '.')
					cnt += 2;
				else if(map[0][col] == 'X' && map[1][col] == 'X') {
					cnt = -1;
					break;
				}
			}	
			if (cnt == 0 || map[0][0] == 'X')
				System.out.println(-1);
			else
				System.out.println(cnt);
		}
		
	}
}
第二题暴力:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int x = input.nextInt();
		int[] nums = new int[n];
		int max = 1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0;i < n;i++) {
			nums[i] = input.nextInt();
			Integer initCount = map.getOrDefault(nums[i], 0);
			initCount += 1;
			map.put(nums[i], initCount);
			max = Math.max(initCount, max);
		}
		map.clear();
		for (int i = 0;i < n;i++) {
			if (map.containsKey(nums[i]))
				continue;
			Integer cnt = 1;
			for (int j = 0;j < n && cnt + n - j - 1 >= max;j++) {
				if (j == i)
					continue;
				if (nums[i] == nums[j] || (nums[j] | x) == nums[i]) {
					cnt++;
				}
			}
			map.put(nums[i], cnt);
			max = Math.max(cnt, max);
		}
		System.out.println(max);
		
	}
}

第三题看错题了,看成了数组的每一个元素都是k的倍数。。。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {	
	public static void main(String[] args) {
		int n,k,L,R;
		BigInteger mod = new BigInteger("1000000007");
		Scanner input = new Scanner(System.in);
		n = input.nextInt();
		k = input.nextInt();
		L = input.nextInt();
		R = input.nextInt();
		BigInteger res = new BigInteger("1");
		
		long count = 0;
		for (int i = L;i <= R;i++) {
			if (i % k == 0)
				count++;
		}
		BigInteger cnt = new BigInteger(String.valueOf(count));

		res = cnt.pow(n).mod(mod);
		System.out.println(res);
	}
}



#美团笔试##笔试题目##美团##Java工程师#
全部评论
A了将近2道了。稳了  能说下第3题思路吗,大佬说是dp
点赞 回复
分享
发布于 2020-03-12 23:18
第一题应该是*2吧
点赞 回复
分享
发布于 2020-03-13 04:31
滴滴
校招火热招聘中
官网直投
三条题呗?楼主报的什么岗
点赞 回复
分享
发布于 2020-03-13 05:29
第三题这都有36哈哈哈
点赞 回复
分享
发布于 2020-03-13 10:01
第二题秒解之后,其他题怎么™都这么难
点赞 回复
分享
发布于 2020-03-13 10:46
第一题45%,第二题AC,剩下三题放空了...🙃自闭
点赞 回复
分享
发布于 2020-03-13 10:49
打扰了,第一题给错代码了,这个代码是后面尝试的只AC了18%,90%的代码我找找看吧
点赞 回复
分享
发布于 2020-03-13 11:03
你好,是21暑期实习么
点赞 回复
分享
发布于 2020-03-13 21:01
没事,我就AC第一题一半都收到面试了,你这么多肯定过了🤣
点赞 回复
分享
发布于 2020-03-13 21:03

相关推荐

3 34 评论
分享
牛客网
牛客企业服务