百度笔试

baidu 格子距离



import java.util.Scanner;

public class Main {
    public static String input = "2\n" +
            "3 3 1 1\n" +
            "8 0 0\n" +
            "2 0 0\n" +
            "1 4 0\n" +
            "2 2 1 1\n" +
            "0 0\n" +
            "0 0\n";
    public static int[][] rst;
    public static int[][] array;
    public static int m; // m行
    public static int n; // n列

    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
        Scanner in = new Scanner(input);
        int caseNum = in.nextInt();
        for (int caseIndex = 0; caseIndex < caseNum; caseIndex++) {
            m = in.nextInt(); // m行
            n = in.nextInt(); // n列
            int startX = in.nextInt();
            int startY = in.nextInt();
            array = new int[m][n];
            rst = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    array[i][j] = in.nextInt();
                    rst[i][j] = -1;
                }
            }
            search(startX - 1, startY - 1, 0);
            print(caseIndex);
        }
    }

    /**
     * 此处应该是广度优先遍历
     */
    private static void search(int x, int y, int stepNum) {
        if (x < 0 || x >= m || y < 0 || y >= n) {
            return;
        }
        // 这个不好
        if (rst[x][y] != -1 && stepNum >= rst[x][y]) {
            return;
        }
        rst[x][y] = stepNum;
        int wall = array[x][y];
        if (wall % 2 != 1) {
            search(x - 1, y, stepNum + 1);
        }
        if ((wall >> 1) % 2 != 1) {
            search(x + 1, y, stepNum + 1);
        }
        if ((wall >> 2) % 2 != 1) {
            search(x, y - 1, stepNum + 1);
        }
        if ((wall >> 3) % 2 != 1) {
            search(x, y + 1, stepNum + 1);
        }
    }

    private static void print(int caseIndex) {
        int caseN = caseIndex + 1;
        System.out.println("Case " + caseN);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(rst[i][j]);
                if (j != n - 1) {
                    System.out.print(" ");
                } else {
                    System.out.println();
                }
            }
        }
    }
}

baidu 找子串


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        System.out.println(find(line));
    }

    private static int find(String str) {
        Set<String> set = new HashSet<>();
        String rst = "";
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if (rst.length() == 0 || rst.contains(aChar + "")) {
                rst += aChar;
            } else {
                rst = aChar + "";
            }
            set.add(rst);
        }
        return set.size();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值