【百度秋招09-14】还是败给了OJ的输入格式和输入类型

2020-09-14 百度客户端开发,3道编程题,自己本地AC了两道。但是第二道由于输入的问题,赛码网只过了9%。

自己之前都是力扣上刷,没注意过这些问题,OJ的不同格式的输入,必须引起重视

字符串数组输入

输入
4
1111
0101
1101
0010

操作后

输出
1111
0111
1111
0010

自己直接就用Scanner类的nextInt();,但是,这个输入每个元素之间是有空格的,直到时间快结束才意识到这点,没来得及改


【输入思路】

  • 先将每一行1111看做字符串,存到String[]一维数组中
  • 然后遍历每一个元素,切割字符串放到二维int[]
  • 不要转换为字符,字符直接转数字比较麻烦,直接切割字符串即可
public static void main(String[] args) {
     
    Scanner in = new Scanner(System.in);
    int side = in.nextInt();
    String[] ss = new String[side];
    //输入字符串数组,next()方法
    for (int i = 0; i < side; i++) {
     
        ss[i] = in.next();
    }
    //切割字符放入二维数组
    int[][] chess = new int[side][side];
    String temp = null;
    for (int i = 0; i < side; i++) {
     
        temp = ss[i];
        for (int j = 0; j < side; j++) {
     
            chess[i][j] = Integer.parseInt(temp.substring(j, j + 1));
        }
    }

	//输出
    for (int i = 0; i < side; i++) {
     
        for (int j = 0; j < side; j++) {
     
            System.out.print(chess[i][j]);
        }
        System.out.println();
    }
}

第一题 字符串

【字符串】: 去除字符串中 连续的字符(含空格)

[输入]
DDDDoo yooouu lllovvvvvvvvvvve                 mmmmee

[输出]
Do you love me

直接遍历字符串,看前后是否相同。将不重复的添加到List集合中,最后StringBuilder拼接

public class SolutionI {
     
    public static void main(String[] args) {
     
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        //String str = "DDDDoo yooouu lllovvvvvvvvvvve                 mmmmee";
        System.out.println(removeRepeat(str));
    }

    private static String removeRepeat(String str) {
     
        String[] ss = str.split("");
        List<String> list = new ArrayList<> ();
        list.add(ss[0]);
        //[D, o,  , y, o, u,  , l, o, v, e,  , m, e]
        for (int i = 1; i < ss.length; i++) {
     
            if(!ss[i].equals(ss[i-1]))  {
     
                list.add(ss[i]);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
     
            sb.append(s);
        }
        return sb.toString();
    }
}



第二题

130. 被围绕的区域 力扣原题

重点是上面的输入格式,int[ ]数组输入只会过9%!!!

/**
 * @Author: iqqcode
 * @Date: 2020-09-14 23:30
 * @Description: 百度笔试题二
 * leetcode-130
 * 

* 【输入问题】: 字符串输入二维数组,转为int[] 数组 */ public class SolutionII { /** * 1111 * 0101 * 1101 * 0010 *

*

* 1111 * 0111 * 1111 * 0010 */ public static void check(int[][] chess) { if (chess == null || chess.length == 0) return; int n = chess.length; int m = chess[0].length; for (int i = 0; i < n; i++) { //将外围的0先标记为中间状态 '2' //第一行, 最后一行 if (i == 0 || i == n - 1) { for (int j = 0; j < m; j++) { fill(i, j, chess, n, m); } } //第一列 fill(i, 0, chess, n, m); //最后一列 fill(i, m - 1, chess, n, m); } //填充被包围的,还原边界临时值 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //先覆盖0, 再还原2.否则还原的2会被新覆盖 if (chess[i][j] == 0) chess[i][j] = 1; if (chess[i][j] == 2) chess[i][j] = 0; } } } private static void fill(int i, int j, int[][] chess, int n, int m) { if (i < 0 || j < 0 || i >= n || j >= m || chess[i][j] == 2 || chess[i][j] == 1) return; //将边界的 0 暂时变为 2 chess[i][j] = 2; fill(i - 1, j, chess, n, m); //上 fill(i, j + 1, chess, n, m); //右 fill(i + 1, j, chess, n, m); //下 fill(i, j - 1, chess, n, m); //左 } public static void main(String[] args) { Scanner in = new Scanner(System.in); int side = in.nextInt(); String[] ss = new String[side]; for (int i = 0; i < side; i++) { ss[i] = in.next(); } int[][] chess = new int[side][side]; String temp = null; for (int i = 0; i < side; i++) { temp = ss[i]; for (int j = 0; j < side; j++) { chess[i][j] = Integer.parseInt(temp.substring(j, j + 1)); } } check(chess); for (int i = 0; i < side; i++) { for (int j = 0; j < side; j++) { System.out.print(chess[i][j]); } System.out.println(); } } }

你可能感兴趣的:(#,【笔试题目】)