9.20度小满Java岗2道算法题

本人战绩:1 + 0.91,许愿给个面试机会
第一题:水(1)
import java.util.Scanner;

public class T1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s1 = in.next();
		String s2 = in.next();
		int len1 = s1.length();
		int len2 = s2.length();
		int res = 0;
		int[] cnt = new int[26];
		for (int i = 0; i < len1; i++)
			cnt[s1.charAt(i) - 'A']++;
		for (int i = 0, cur; i < len2; i++) {
			cur = s2.charAt(i) - 'A';
			if (cnt[cur] > 0) {
				cnt[cur]--;
				res++;
			}
		}
		System.out.println(res);
	}
}
第二题:0.91(超时=。=),有大佬知道我这程序哪里可以优化的么,欢迎各位大佬评论区讨论🤣
我的思路:记录从起点'@'到达各个坐标点需要用到最小技能个数。
import java.util.Arrays;
import java.util.Scanner;

public class T2 {
	static boolean flag;
	static int ans;
	static int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
	static boolean[][] vis;
	static int[][] cost;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		int n, m, sx, sy;
		char[][] map;

		while (T-- > 0) {
			n = in.nextInt();
			m = in.nextInt();
			in.nextLine();
			map = new char[n][m];
			vis = new boolean[n][m];
			cost = new int[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.next().toCharArray();
				Arrays.fill(cost[i], 0x3f3f3f3f);
			}
//			for (int i = 0; i < n; i++)
//				for (int j = 0; j < m; j++)
//					System.out.print(map[i][j] + (j == m - 1 ? "\n" : " "));
			sx = 0;
			sy = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (map[i][j] == '@') {
						sx = i;
						sy = j;
					}
				}
			}
			flag = false;
			vis[sx][sy] = true;
			dfs(sx, sy, map, n, m);
			if (flag) {
//				System.out.println("cdscsdcs");
				System.out.println(0);
				continue;
			}
			flag = false;
			ans = Integer.MAX_VALUE;
			cost[sx][sy] = 0;
			dfs(sx, sy, map, n, m, cost[sx][sy]);
			System.out.println(flag ? ans : -1);
		}
	}

	static void dfs(int x, int y, char[][] map, int n, int m) {
		if (flag)
			return;
		for (int i = 0; i < 4; i++) {
			int dx = x + dir[i][0], dy = y + dir[i][1];
			// 出界了
			if (!check(dx, dy, n, m)) {
				flag = true;
			} else if (!vis[dx][dy] && map[dx][dy] == '.') {
				vis[dx][dy] = true;
				dfs(dx, dy, map, n, m);
			}
		}
	}

	static void dfs(int x, int y, char[][] map, int n, int m, int cnt) {
		for (int i = 0; i < 4; i++) {
			int dx = x + dir[i][0], dy = y + dir[i][1];
			// 出界了
			if (!check(dx, dy, n, m)) {
				flag = true;
				ans = Math.min(ans, cnt);
			} else if (map[dx][dy] != '#') {
				if (map[dx][dy] == '*' && cost[x][y] + 1 < cost[dx][dy]) {
					cost[dx][dy] = cost[x][y] + 1;
					dfs(dx, dy, map, n, m, cost[dx][dy]);
				} else if (map[dx][dy] == '.' && cost[x][y] < cost[dx][dy]) {
					cost[dx][dy] = cost[x][y];
					dfs(dx, dy, map, n, m, cost[dx][dy]);
				}
			}
		}
	}

	static boolean check(int x, int y, int n, int m) {
		return x >= 0 && y >= 0 && x < n && y < m;
	}
}


#笔试题目##度小满#
全部评论
楼主厉害,我只用了dfs,没有考虑使用路径备忘录导致超时了😪
点赞 回复 分享
发布于 2020-09-20 22:01
我擦,我字节和度小满一起刚。 然后字节多用了一点时间,结果做度小满第二题的时候,也是你这样做的,代码量有点大,没时间写完,好气。。。。。。。。。。
点赞 回复 分享
发布于 2020-09-20 21:49

相关推荐

点赞 评论 收藏
分享
真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务