题解 | #二维数组操作#

二维数组操作

https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4

import java.util.*;
import java.util.stream.Collectors;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            printTheResult(in);
            printTheResult(in);
        }
    }


    public static void printTheResult(Scanner in) {
        List<Integer> mn = getAction(in);

        // 生成 mn坐标
        outResult(mn.get(0) > 9 || mn.get(0) < 1 || mn.get(1) > 9 || mn.get(1) < 1);

        // 互换
        List<Integer> xySwitch = getAction(in);
        boolean noValid = (xySwitch.get(0) >= mn.get(0) || xySwitch.get(0) < 0) ||
                          (xySwitch.get(1) >= mn.get(1) || xySwitch.get(1) < 0) ||
                          (xySwitch.get(2) >= mn.get(0) || xySwitch.get(2) < 0) ||
                          (xySwitch.get(3) >= mn.get(1) || xySwitch.get(1) < 0);
        outResult(noValid);

        // 插入X行
        List<Integer> insertX = getAction(in);
        noValid = mn.get(0) == 9 || insertX.get(0) < 0 || insertX.get(0) >= mn.get(0);
        outResult(noValid);

        //插入Y列
        List<Integer> insertY = getAction(in);
        noValid = mn.get(1) == 9 || insertY.get(0) < 0 || insertY.get(0) >= mn.get(1);
        outResult(noValid);

        //查找某行
        List<Integer> search = getAction(in);
        noValid = search.get(0) >= mn.get(0) || search.get(0) < 0 ||
                  search.get(1) >= mn.get(1) || search.get(1) < 0;
        outResult(noValid);
    }

    public static void outResult(boolean noValid) {
        if (noValid) {
            System.out.println("-1");
        } else {
            System.out.println(0);
        }
    }

    public static List<Integer> getAction(Scanner in) {
        String a = in.nextLine();
        String[] mn = a.split(" ");
        List<Integer> mnA = Arrays.asList(mn).stream().map(e -> Integer.parseInt(
                                e)).collect(Collectors.toList());
        return mnA;
    }

}

全部评论

相关推荐

耀孝女:就是你排序挂了
点赞 评论 收藏
分享
过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务