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;
	}
}


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

相关推荐

希望被捞的猫头鹰很理智:大概率待遇低怕硕士跑路
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客企业服务