Leetcode-Java (三)

前言

每天五道LeetCode,算是为了明年的春招做点准备吧。在此分享出来和大家一起学习。如果有什么疑问可以在下面评论,我看到了会及时回复的。
今天是第三天。

Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation
of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in
ascending order).
e replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the le-hand column and its corresponding outputs are in the
right-hand column.
1,2,31,3,2
3,2,11,2,3
1,1,51,5,1

/**
     * 2.1.11 Next Permutation
     */
    public static int[] findNextPermutation(int[] A) {
        if (A == null) {
            return null;
        }
        int i = A.length - 2;
        while (i > 0 && A[i] > A[i + 1]) {
            i--;
        }
        if (i != 0) {
            int j = 0;
            for (j = A.length - 1; j > 0; j--) {
                if (i != j && A[j] > A[i]) {
                    int temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                    break;
                }
            }
            reverse(A, j);
        } else {
            reverse(A, i);
        }
        return A;
    }

    public static void reverse(int[] A, int begin) {
        int end = A.length - 1;
        while (begin < end) {
            int temp = A[begin];
            A[begin] = A[end];
            A[end] = temp;
            end--;
            begin++;
        }
    }

    public static void findNextPermutationTest() {
        int[] A = {2, 3, 6, 5, 4, 1};
        int[] nextPermutation = findNextPermutation(A);
        System.out.println(Arrays.toString(nextPermutation));
    }

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - e Rules
http://sudoku.com.au/TheRules.aspx .
e Sudoku board could be partially filled, where empty cells are filled with the character '.'.

/**
     * 2.1.13 Valid Sudoku
     */
    public static boolean isValidSudoku(char[][] board) {
        int grid_row = 0;
        int grid_column = 0;
        for (int i = 0; i < 9; i++) {
            //检查行
            Set row = new HashSet();
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.' && row.contains(board[i][j])) {
                    return false;
                } else {
                    row.add(board[i][j]);
                }
            }
            //检查列
            Set column = new HashSet();
            for (int j = 0; j < 9; j++) {
                if (board[j][i] != '.' && column.contains(board[j][i])) {
                    return false;
                } else {
                    column.add(board[j][i]);
                }
            }
            //检查3*3的格子
            Set grid = new HashSet();
            if (i < 3) {
                grid_column = i * 3;
            } else if (i < 6) {
                grid_row = 3;
                grid_column = (i - 3) * 3;
            } else if (i < 9) {
                grid_row = 6;
                grid_column = (i - 6) * 3;
            }
            for (int m = 0; m < 3; m++) {
                for (int n = 0; n < 3; n++) {
                    if (board[m + grid_row][n + grid_column] != '.' && grid.contains(board[m + grid_row][n + grid_column])) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static void isValidSudokuTest() {
        char[][] board = {
                {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}
        };
        boolean isValid = isValidSudoku(board);
        System.out.println("isValid : " + isValid);
    }

Tapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute
how much water it is able to trap aer raining.
For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.


    /**
     * 2.1.14 Tapping Rain Water
     *
     */
    public static int trap(int[] height) {
        int size = height.length;
        int result = 0;
        if(size <= 0){
            return result;
        }
        int[] leftMax = new int[size];
        int[] rightMax = new int[size];
        leftMax[0] = 0;
        rightMax[size-1] = 0;
        for(int i = 1;i < size;++i){
            leftMax[i] = Math.max(leftMax[i-1],height[i]);
            rightMax[size-1-i] = Math.max(rightMax[size-i],height[size-1-i]);
        }
        int h;
        for(int i = 0;i < size;++i){
            h = Math.min(leftMax[i],rightMax[i]);
            if(h > height[i]){
                result += h - height[i];
            }
        }
        return result;
    }
    public static void trapTest() {
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        int water = trap(height);
        System.out.println("water : " + water);
    }

Rotate Image

You are given an n × n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up: Could you do this in-place?

/**
     * 2.11.5 Rotate Image
     */
    public static void rotate(int[][] matrix){
        int N = matrix.length;
        for (int i =0;i<N;i++) {
            for (int j = i+1; j < N; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        for (int i = 0;i<N;i++){
            for (int j = 0; j<N/2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][N-1-j];
                matrix[i][N-1-j] = temp;
            }
        }
    }
    public static void rotateTest(){
        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        rotate(matrix);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值