[1/30] MIOJ刷题:和为零的三元组/数独游戏

  1. 和为零的三元组
    地址:MIOJ 15. 和为零的三元组
package lambdasinaction.playground.mioj;

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

/**
 * @author li2niu
 * @date 2020/10/21
 * @Description
 */


public class TriElementsEquals0 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String line;
        while (scan.hasNextLine()) {
            line = scan.nextLine().trim();
            List elements = Arrays.stream(line.split(",")).map(Integer::parseInt).collect(Collectors.toList());
            int size = elements.size();
            if (size < 3) {
                System.out.println(0);
                return;
            }
            Set answerSetList = new HashSet<>();
            getAnswer1(answerSetList, elements);
            System.out.println(answerSetList.size());

            Set answerSetList2 = new HashSet<>();
            getAnswer2(answerSetList2, elements);
            System.out.println(answerSetList2.size());


        }
    }

    /**
     * 先确定中间的数字,然后两边的数字左右移动调节寻找符合条件的三元组
     * O(n^2)
     * @param answerSetList
     * @param elements
     */
    private static void getAnswer1(Set answerSetList, List elements) {
        int maxIndex, minIndex;
        List sortedList = elements.parallelStream().sorted().collect(Collectors.toList());
        int size = sortedList.size();
        for (int i = 1; i < size - 1; i++) {
            minIndex = i - 1;
            maxIndex = i + 1;
            while (minIndex >= 0 && maxIndex < size) {
                int sum = sortedList.get(i) + sortedList.get(minIndex) + sortedList.get(maxIndex);
                if (sum == 0) {
                    String result = sortedList.get(minIndex) + "," + sortedList.get(i) + "," + sortedList.get(maxIndex);
                    answerSetList.add(result);
                    minIndex--;
                } else if (sum > 0) {
                    minIndex--;
                } else {
                    maxIndex++;
                }
            }
        }
    }

    /**
     * 枚举所有的组合然后寻找符合条件的三元组
     * O(n*(n-1)*(n-2))=> O(n^3)  (*☻-☻*))
     * @param answerSetList
     * @param elements
     */
    private static void getAnswer2(Set answerSetList, List elements) {
        int size = elements.size();
        for (int i = 0; i < size; i++) {
            Integer element = elements.get(i);
            Queue queue = new LinkedList<>();
            for (int j = 0; j < size; j++) {
                if (j != i) {
                    queue.add(elements.get(j));
                }
            }
            Integer pop1;
            int poll1Count = 0;
            while (poll1Count <= queue.size() && (pop1 = queue.poll()) != null) {
                poll1Count++;
                Integer pop2;
                int poll2Count = 0;
                while (poll2Count <= queue.size() - 1 && (pop2 = queue.poll()) != null) {
                    poll2Count++;
                    if (pop1 + pop2 + element == 0) {
                        String result = Stream.of(pop1, element, pop2)
                                .sorted()
                                .map(String::valueOf)
                                .collect(Collectors.joining(","));
                        answerSetList.add(result);
                    }
                    queue.add(pop2);
                }
                queue.add(pop1);
            }
        }
    }
}

  1. 数独游戏
    地址:MIOJ 45.数独游戏
    leetcode Valid sudoku
    这题有错误
    leetcode版本
package playground.leetcode.sudoku;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author lichuanyi
 * @date 2020/10/22
 * @Description
 */
public class Solution {
    private static final String case1 = "8,3,.,.,7,.,.,.,. 6,.,.,1,9,5,.,.,. .,9,8,.,.,.,.,6,. 8,.,.,.,6,.,.,.,3 4,.,.,8,.,3,.,.,1 7,.,.,.,2,.,.,.,6 .,6,.,.,.,.,2,8,. .,.,.,4,1,9,.,.,5 .,.,.,.,8,.,.,7,9";
    private static final String case2 = "5,3,.,.,7,.,.,.,. 6,.,.,1,9,5,.,.,. .,9,8,.,.,.,.,6,. 8,.,.,.,6,.,.,.,3 4,.,.,8,.,3,.,.,1 7,.,.,.,2,.,.,.,6 .,6,.,.,.,.,2,8,. .,.,.,4,1,9,.,.,5 .,.,.,.,8,.,.,7,9";
    private static final String case3 = ".,.,.,.,5,.,.,1,. .,4,.,3,.,.,.,.,. .,.,.,.,.,3,.,.,1 8,.,.,.,.,.,.,2,. .,.,2,.,7,.,.,.,. .,1,5,.,.,.,.,.,. .,.,.,.,.,2,.,.,. .,2,.,9,.,.,.,.,. .,.,4,.,.,.,.,.,.";

    private static final String case4 = "5,3,.,.,7,.,.,.,. 6,.,.,1,9,5,.,.,. .,9,8,.,.,.,.,6,. 8,.,.,.,6,.,.,.,3 4,.,.,8,.,3,.,.,1 7,.,.,.,2,.,.,.,6 .,6,.,.,.,.,2,8,. .,.,.,4,1,9,.,.,5 .,.,.,.,8,.,.,7,9";

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String line;
        while (scan.hasNextLine()) {
            line = scan.nextLine().trim();
            String[] sudokuLines = line.split(" ");

            char[][] board = initBoard(sudokuLines);
            printBoard(board);
            System.out.println(new Solution().isValidSudoku(board));
        }
    }

    private static void printBoard(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print(board[i][j]+" ");
            }
            System.out.println();
        }
    }

    private static char[][] initBoard(String[] sudokuLines) {
        char[][] board = new char[9][9];
        for (int i = 0; i < 9; i++) {
            String line = sudokuLines[i];
            final String[] stones = line.split(",");
            for (int j = 0; j < 9; j++) {
                board[i][j] = ".".equals(stones[j]) ? '.' : stones[j].charAt(0);
            }
        }
        return board;
    }

    public boolean isValidSudoku(char[][] board) {
        Map map = new HashMap<>(9);
        boolean result = validateRows(board, map);
        if (!result) {
            return false;
        }
        map.clear();
        result = validateColumns(board, map);
        if (!result) {
            return false;
        }
        map.clear();
        result = validate9Cells(board, map);
        return result;
    }

    private boolean validateRows(char[][] board, Map map) {
        for (int i = 0; i < 9; i++) {
            map.clear();
            for (int j = 0; j < 9; j++) {
                if (map.containsKey(board[i][j])) {
                    return false;
                }
                if (board[i][j] != '.') {
                    map.put(board[i][j], 0);
                }
            }
        }
        return true;
    }

    /**
     * 校验九个小方格
     *
     * @param board
     * @param map
     */
    private static boolean validate9Cells(char[][] board, Map map) {
        for (int i = 0; i < 3; i++) {
            for (int l = 0; l < 3; l++) {
                map.clear();
                for (int j = 0; j < 3; j++) {
                    for (int k = 0; k < 3; k++) {
                        char stone = board[i * 3 + j][3*l+k];
                        if (map.containsKey(stone)) {
                            return false;
                        }
                        if (stone != '.') {
                            map.put(stone, 0);
                        }
                    }
                }
            }

        }
        return true;
    }

    /**
     * 校验列
     *
     * @param board
     * @param map
     */
    private static boolean validateColumns(char[][] board, Map map) {
        for (int i = 0; i < 9; i++) {
            map.clear();
            for (int j = 0; j < 9; j++) {
                char stone = board[j][i];
                if (map.containsKey(stone)) {
                    return false;
                }
                if (stone != '.') {
                    map.put(stone, 0);
                }
            }
        }
        return true;
    }

    /**
     * 初始化棋盘同时按行校验
     *
     * @param sudokuLines
     * @param map
     * @return
     */
    private static int[][] initBoardAndValidateRows(String[] sudokuLines, Map map) {
        int[][] board = new int[9][9];
        for (int i = 0; i < 9; i++) {
            String line = sudokuLines[i];
            final String[] stones = line.split(",");
            map.clear();
            for (int j = 0; j < 9; j++) {
                board[i][j] = "-".equals(stones[j]) ? 0 : Integer.parseInt(stones[j]);
                if (map.containsKey(board[i][j])) {
                    System.out.println(false);
                    return new int[0][];
                }
                if (board[i][j] != 0) {
                    map.put(board[i][j], 0);
                }
            }
        }
        return board;
    }
}

mioj版本

package playground.mioj.day1.sudoku;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author: lichuanyi
 * @description:
 * @date: 2020/10/21 23:35
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String line;
        while (scan.hasNextLine()) {
            line = scan.nextLine().trim();
            String[] sudokuLines = line.split(" ");
            Map map = new HashMap<>(9);
            int[][] board = initBoardAndValidateRows(sudokuLines,map);
            if (board.length == 0){
                return;
            }
//            else{
//                for (int i = 0; i < 9; i++) {
//                    for (int j = 0; j < 9; j++) {
//                        System.out.print(board[i][j]+" ");
//                    }
//                    System.out.println();
//                }
//            }
            map.clear();
            boolean result = validateColumns(board,map);
            if (!result){
                System.out.println(false);
                return;
            }
            map.clear();
            result = validate9Cells(board,map);
            System.out.println(result);
        }
    }

    /**
     * 校验九个小方格
     * @param board
     * @param map
     */
    private static boolean validate9Cells(int[][] board, Map map) {
        for (int i = 0; i < 3; i++) {
            for (int l = 0; l < 3; l++) {
                map.clear();
                for (int j = 0; j < 3; j++) {
                    for (int k = 0; k < 3; k++) {
                        int stone = board[i*3+j][l*3+k];
                        if (stone!= 0 && map.containsKey(stone)){
                            return false;
                        }
                        map.put(stone,0);
                    }
                }
            }

        }
        return true;
    }

    /**
     * 校验列
     * @param board
     * @param map
     */
    private static boolean validateColumns(int[][] board, Map map) {
        for (int i = 0; i < 9; i++) {
            map.clear();
            for (int j = 0; j < 9; j++) {
                int stone = board[j][i];
                if (stone != 0 && map.containsKey(stone)){
                    return false;
                }
                map.put(stone,0);
            }
        }
        return true;
    }

    /**
     * 初始化棋盘同时按行校验
     * @param sudokuLines
     * @param map
     * @return
     */
    private static int[][] initBoardAndValidateRows(String[] sudokuLines, Map map) {
        int[][] board = new int[9][9];
        for (int i = 0; i < 9; i++) {
            String line = sudokuLines[i];
            final String[] stones = line.split(",");
            map.clear();
            for (int j = 0; j < 9; j++) {
                board[i][j] = "-".equals(stones[j]) ? 0 : Integer.parseInt(stones[j]);
                if (board[i][j]!= 0 && map.containsKey(board[i][j])){
                    System.out.println(false);
                    return new int[0][];
                }
                map.put(board[i][j],0);
            }
        }
        return board;
    }
}


//5,1,-,6,-,-,-,9,8 -,7,-,1,9,4,-,-,- -,-,-,-,-,-,-,6,- 8,-,-,4,-,-,7,-,- -,6,-,8,-,3,-,2,- -,-,3,-,-,1,-,-,6 -,3,-,-,-,-,-,4,- -,-,-,3,1,9,-,8,- 2,8,-,-,-,5,-,7,9

你可能感兴趣的:([1/30] MIOJ刷题:和为零的三元组/数独游戏)