• 笔试训练2


    牛客.单词搜索 

    刚开始我就想是搜索,但是不清楚bfs还是dfs更好,我尝试了bfs但是队列存东西,没有我想象的那么好写,所以我决定试试dfs 

    1. import java.util.*;
    2. public class Solution {
    3. static int m = 0;
    4. static int n = 0;
    5. static int []dx = {1, -1, 0, 0};
    6. static int []dy = {0, 0, 1, -1};
    7. static boolean [][]vis;
    8. static char[]word;
    9. /**
    10. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    11. *
    12. *
    13. * @param board string字符串一维数组
    14. * @param word string字符串
    15. * @return bool布尔型
    16. */
    17. public static boolean exist (String[] board, String _word) {
    18. m = board.length;
    19. n = board[0].length();
    20. word = _word.toCharArray();
    21. char[][]a = new char[m][n];
    22. vis = new boolean[m][n];
    23. for (int i = 0; i < m; i++) {
    24. for (int j = 0; j < n; j++) {
    25. if (board[i].charAt(j) == word[0]) {
    26. vis[i][j] = true; //标记
    27. if (dfs(board, i, j, 0) == true) {
    28. return true;
    29. };
    30. vis[i][j]=false; //回溯
    31. }
    32. }
    33. }
    34. return false;
    35. }
    36. private static boolean dfs(String[] board, int i, int j, int pos) {
    37. if (pos == word.length - 1) {
    38. return true;
    39. }
    40. for (int ii = 0; ii < 4; ii++) {
    41. int x = i + dx[ii];
    42. int y = j + dy[ii];
    43. //注意这里是pos+1传递的是第几个下标
    44. if (x >= 0 && x < m && y >= 0 && y < n && board[x].charAt(y) == word[pos + 1] &&
    45. vis[x][y] == false) {
    46. vis[x][y] = true; //标记
    47. if ( dfs(board, x, y, pos + 1) == true) {
    48. return true;
    49. };
    50. vis[x][y] = false; //回溯
    51. }
    52. }
    53. return false;
    54. }
    55. }

    牛客.杨辉三角

    还想起来我第一次处理这个的时候,怎么想也不知道怎么写,那时候我是大一下学期刚学数据结构,然后到了今天,虽然我不是特别厉害,但是还是有所进步的,这个问题的想法,由于它是由上一行的这个和上一行的左边这个加一起,我的想法瞬间就想起来了dp,于是使用了dp处理这个问题

    1. import java.util.*;
    2. // 注意类名必须为 Main, 不要有任何 package xxx 信息
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. // 注意 hasNext 和 hasNextLine 的区别
    7. int a = scanner.nextInt();
    8. int[][]b = new int[a + 1][a + 1];
    9. for (int i = 0; i <= a; i++)
    10. b[i][0] = 1;
    11. for (int i = 1; i <= a; i++) {
    12. for (int j = 1; j <= i; j++) {
    13. b[i][j] = b[i - 1][j - 1] + b[i - 1][j];
    14. }
    15. }
    16. for (int i = 0; i < a; i++) {
    17. for (int j = 0; j <= i; j++) {
    18. if (b[i][j] != 0) {
    19. StringBuffer ret = new StringBuffer();
    20. int len = Integer.toString(b[i][j]).length();
    21. for (int k = 0; k < 5 - len; k++)
    22. ret.append(" ");
    23. System.out.print(ret.toString() + b[i][j]);
    24. }
    25. }
    26. if (i + 1 != a)
    27. System.out.println();
    28. }
    29. }
    30. }

    牛客.游游的you

    输入输出是因为我想熟悉一下模版,这道题,不用也可以。就是看连续的o就-1,先找对应的you,再去找oo 

    1. import java.util.*;
    2. import java.io.BufferedReader;
    3. import java.io.IOException;
    4. import java.io.InputStreamReader;
    5. import java.util.StringTokenizer;
    6. import java.io.OutputStreamWriter;
    7. import java.io.PrintWriter;
    8. import java.io.BufferedWriter;
    9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
    10. public class Main {
    11. public static PrintWriter out = new PrintWriter(new BufferedWriter(
    12. new OutputStreamWriter(System.out)));
    13. public static void main(String[] args)throws IOException {
    14. Read scanner = new Read();
    15. int a1 = scanner.nextInt();
    16. int[][] a = new int[a1][3];
    17. int[] dp = new int[a1];
    18. int[] count = new int[a1];
    19. for (int i = 0; i < a1; i++) {
    20. int min = Integer.MAX_VALUE;
    21. for (int j = 0; j < 3; j++) {
    22. a[i][j] = scanner.nextInt();
    23. min = Math.min(a[i][j], min);
    24. }
    25. count[i] = a[i][1];
    26. dp[i] = min;
    27. }
    28. int sum = 0;
    29. for (int i = 0; i < a1; i++) {
    30. if (count[i] - dp[i] - 1 <= 0)
    31. sum = dp[i] * 2;
    32. else sum = dp[i] * 2 + (count[i] - dp[i] - 1) ;
    33. System.out.println(sum);
    34. }
    35. // 注意 hasNext 和 hasNextLine 的区别
    36. }
    37. }
    38. class Read {
    39. StringTokenizer st = new StringTokenizer("");
    40. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    41. String next()throws IOException {
    42. while (!st.hasMoreTokens()) {
    43. st = new StringTokenizer(bf.readLine());
    44. }
    45. return st.nextToken();
    46. }
    47. String nextLine()throws IOException {
    48. return bf.readLine();
    49. }
    50. int nextInt() throws IOException {
    51. return Integer.parseInt(next());
    52. }
    53. long nextLong()throws IOException {
    54. return Long.parseLong(next());
    55. }
    56. double nextDouble()throws IOException {
    57. return Double.parseDouble(next());
    58. }
    59. }

    牛客.腐烂的苹果

    常规的bfs,使用vis来标记,然后就直接宽度搜索,用一个队列,放循环里面,然后,分两块往下面遍历(因为这是1s,所以把它放到sz出去,因为这个sz的作用是用来,统计一个烂苹果能感染多少组好苹果,然后把这些好苹果放到里面) 

    1. import java.util.*;
    2. public class Solution {
    3. /**
    4. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    5. *
    6. *
    7. * @param grid int整型ArrayList>
    8. * @return int整型
    9. */
    10. static int []dx = {0, 0, 1, -1};
    11. static int []dy = {1, -1, 0, 0};
    12. public static int rotApple (ArrayList<ArrayList<Integer>> grid) {
    13. int m = grid.size();
    14. int n = grid.get(0).size();
    15. int ret = 0;
    16. boolean[][]vis = new boolean[m][n];
    17. Queue<int[]>q = new LinkedList<>();
    18. for (int i = 0; i < m; i++) {
    19. for (int j = 0; j < n; j++) {
    20. if (grid.get(i).get(j) == 2) {
    21. q.add(new int[] {i, j});
    22. vis[i][j] = true;
    23. }
    24. }
    25. }
    26. while (!q.isEmpty()) {
    27. int sz = q.size();
    28. while (sz > 0) {
    29. int []k = q.poll();
    30. sz--;
    31. for (int i = 0; i < 4; i++) {
    32. int x = dx[i] + k[0];
    33. int y = dy[i] + k[1];
    34. if (x >= 0 && x < m && y >= 0 && y < n && grid.get(x).get(y) == 1 &&
    35. vis[x][y] == false) {
    36. q.add(new int[] {x, y});
    37. vis[x][y] = true;
    38. }
    39. }
    40. }
    41. ret++;
    42. }
    43. for (int i = 0; i < m; i++) {
    44. for (int j = 0; j < n; j++) {
    45. if (grid.get(i).get(j) == 1 && vis[i][j] == false) {
    46. return -1;
    47. }
    48. }
    49. }
    50. return ret - 1;
    51. }
    52. // write code her
    53. }
  • 相关阅读:
    基于Spring Boot的中小型医院网站的设计与实现源码(springboot)
    牛客网《剑指offer》专栏刷题练习之双指针算法的使用
    开源电商项目 Mall:构建高效电商系统的终极选择
    Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
    生成式AI爆发,安全问题如何解决?
    mybatis中判断传入的数组与集合是否为空+mybatis中Foreach的使用详解
    Apple硬件相关
    计算机网络-传输层
    华为数据库工程师面试题目
    海大校园学习《乡村振兴战略下传统村落文化旅游设计》许少辉八一新著
  • 原文地址:https://blog.csdn.net/weixin_72953218/article/details/139079474