Jerry's Blog

Recording what I learned everyday

View on GitHub


2 October 2019

LeetCode(25) -- 657, 661, 665

by Jerry Zhang

P657. Robot Return to Origin (Easy)

机器人可以上下左右移动。问经过一些移动后,是否回到原点。

Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

我的思路

数一下U和D数量是否相等,L和R数量是否相等就好了。

我的代码

class Solution {
    public boolean judgeCircle(String moves) {
        int h = 0, v = 0;
        char[] chars = moves.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char move = chars[i];
            if (move == 'U') {
                v++;
            } else if (move == 'D') {
                v--;
            } else if (move == 'L') {
                h--;
            } else if (move == 'R') {
                h++;
            }
        }
        return h == 0 && v == 0;
    }
}

94%

最优解

class Solution {
    public boolean judgeCircle(String moves) {
        char arr[]=moves.toCharArray();
        int u=counter(arr,'U');
        int d=counter(arr,'D');
        int l=counter(arr,'L');
        int r=counter(arr,'R');
        if(!(u-d==0 && l-r==0)){
            return false;
        }
        return true;
    }

    public int counter(char arr[],char c){
        int count=0;
        for(char ch:arr){
            if(ch==c){
                count++;
            }
        }
        return count;
    }
}

P661. Image Smoother (Easy)

二维数组,表示一个图片的灰度。使每一个方格,都变成它周围所有方格(包括它本身)的平均数,向下取整。

我的思路

周围的8个点,只有满足条件的才加到sum里面。

我的代码

public class E_661_ImageSmoother {
    public int[][] imageSmoother(int[][] M) {
        int[][] res = new int[M.length][M[0].length];
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                int sum = 0, count = 1;
                if (i > 0 && j > 0) {
                    sum += M[i-1][j-1];
                    count++;
                }
                if (i < M.length - 1 && j < M[0].length - 1) {
                    sum += M[i+1][j+1];
                    count++;
                }
                if (i < M.length - 1 && j > 0) {
                    sum += M[i+1][j-1];
                    count++;
                }
                if (i > 0 && j < M[0].length - 1) {
                    sum += M[i-1][j+1];
                    count++;
                }
                if (i > 0) {
                    sum += M[i-1][j];
                    count++;
                }
                if (j > 0) {
                    sum += M[i][j-1];
                    count++;
                }
                if (i < M.length - 1) {
                    sum += M[i+1][j];
                    count++;
                }
                if (j < M[0].length - 1) {
                    sum += M[i][j+1];
                    count++;
                }
                sum += M[i][j];
                res[i][j] = (int) Math.floor((double) sum / count);
            }
        }
        return res;
    }
}

62%

最优解

class Solution {
    public int[][] imageSmoother(int[][] M) {
        int[][] result = new int[M.length][M[0].length];
        for(int i = 0; i < M.length; i++) {
            for(int j = 0; j < M[0].length; j++) {
                result[i][j] = calculateAverageNearby(M, i, j);
            }
        }
        return result;
    }

    private int calculateAverageNearby(int[][] M, int r, int c) {
        int sum = 0;
        sum += M[r][c];
        int count = 1;
        if(r - 1 >= 0) {
            sum += M[r-1][c];
            count++;
        }
        if(r + 1 < M.length) {
            sum += M[r+1][c];
            count++;
        }
        if(c - 1 >= 0) {
            sum += M[r][c-1];
            if(r - 1 >= 0) {
                sum += M[r-1][c-1];
                count++;
            }
            if(r + 1 < M.length) {
                sum += M[r+1][c-1];
                count++;
            }
            count++;
        }
        if(c + 1 < M[0].length) {
            sum += M[r][c+1];
            if(r - 1 >= 0) {
                sum += M[r-1][c+1];
                count++;
            }
            if(r + 1 < M.length) {
                sum += M[r+1][c+1];
                count++;
            }
            count++;
        }
        return sum/count;
    }
}

在判断出来列数大于0后,又分了一下类。这样判断的次数比我少一点。

P665. Non-decreasing Array (Easy)

给一个数组。能否通过只修改一个数,就让这个数组变成一个升序的数组。

我的思路

就查一遍有几个下降的。

光查下降是不够的。下降之后如果无法调整,那就直接终止。

我的代码

public class E_665_Non_decreasingArray {
    public boolean checkPossibility(int[] nums) {
        int count = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] > nums[i]) {
                if (count == 1) return false;
                count++;
                if (i > 1 && nums[i] < nums[i - 2] && i < nums.length - 1 && nums[i + 1] < nums[i - 1]) {
                    return false;
                }
            }
        }
        return true;
    }
}

99%

最优解

class Solution {
     public boolean checkPossibility(int[] nums) {
        int previous = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] >= previous) {
                previous = nums[i];
            } else {
                if (count == 0) {
                    count++;
                    if (i < 2) {
                        previous = nums[1];
                        continue;
                    }
                    if (i >= 2 && nums[i] >= nums[i-2]) {
                        previous = nums[i];
                        continue;
                    }
                    if (i < nums.length-1 && nums[i-1] <= nums[i+1]) {
                        previous = nums[i-1];
                        continue;
                    }
                    if (i == nums.length-1) return true;
                    else return false;
                }
                else return false;
            }
        }
        return true;
    }
}
tags: LeetCode