头像

kidforever911




离线:2天前


最近来访(43)
用户头像
zrq071211
用户头像
心升明月
用户头像
小时
用户头像
李玉中
用户头像
慕元思靖
用户头像
xutao2009
用户头像
球子巨能睡
用户头像
limie
用户头像
L.妍ab
用户头像
Alphaville
用户头像
suh
用户头像
longwu龙舞
用户头像
sep
用户头像
只烟泽
用户头像
__Rain
用户头像
阿星爱吃锅包肉
用户头像
二十九
用户头像
QianMo_QAQ
用户头像
TYUTICPC01
用户头像
aoczs


class Solution {
    public int integerBreak(int n) {
        if(n <= 3) return 1 * (n - 1);
        int result = 1;
        if(n % 3 == 1){
            result *= 4;
            n -= 4;
        }
        if(n % 3 == 2){
            result *= 2;
            n -= 2;
        }
        while(n > 0){
            result *= 3;
            n -= 3;
        }
        return result;
    }
}


活动打卡代码 AcWing 25. 剪绳子

class Solution {
    public int integerBreak(int n) {
        if(n <= 3) return 1 * (n - 1);
        int result = 1;
        if(n % 3 == 1){
            result *= 4;
            n -= 4;
        }
        if(n % 3 == 2){
            result *= 2;
            n -= 2;
        }
        while(n > 0){
            result *= 3;
            n -= 3;
        }
        return result;
    }
}



class Solution {
    private boolean[][] visited;
    private int direction[][] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};

    private static class Node{
        int first;
        int second;

        public Node(int first, int second){
            this.first = first;
            this.second = second;
        }
    }

    public int movingCount(int threshold, int rows, int cols)
    {
        if(rows < 1 || cols < 1) return 0;
        visited = new boolean[rows][cols];
        visited[0][0] = true;
        Queue<Node> record = new LinkedList<>();
        record.add(new Node(0, 0));
        int result = 0;
        while(!record.isEmpty()){
            Node tempNode = record.poll();
            result ++;

            for(int i = 0; i < 4; i ++){
                int newx = tempNode.first + direction[i][0];
                int newy = tempNode.second + direction[i][1];
                if(newx >=0 && newx < rows && newy >= 0 && newy < cols && getSum(newx, newy) <= threshold && !visited[newx][newy]){
                    record.add(new Node(newx, newy));
                    visited[newx][newy] = true;
                }
            }
        }
        return result;
    }

    private int getSingle(int x){
        int sum = 0;
        while(x > 0){
            sum += x % 10;
            x /= 10;
        }
        return sum;
    }

    private int getSum(int x, int y){
        return getSingle(x) + getSingle(y);
    }

}



class Solution {
    private boolean[][] visited;
    private int direction[][] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};

    private static class Node{
        int first;
        int second;

        public Node(int first, int second){
            this.first = first;
            this.second = second;
        }
    }

    public int movingCount(int threshold, int rows, int cols)
    {
        if(rows < 1 || cols < 1) return 0;
        visited = new boolean[rows][cols];
        visited[0][0] = true;
        Queue<Node> record = new LinkedList<>();
        record.add(new Node(0, 0));
        int result = 0;
        while(!record.isEmpty()){
            Node tempNode = record.poll();
            result ++;

            for(int i = 0; i < 4; i ++){
                int newx = tempNode.first + direction[i][0];
                int newy = tempNode.second + direction[i][1];
                if(newx >=0 && newx < rows && newy >= 0 && newy < cols && getSum(newx, newy) <= threshold && !visited[newx][newy]){
                    record.add(new Node(newx, newy));
                    visited[newx][newy] = true;
                }
            }
        }
        return result;
    }

    private int getSingle(int x){
        int sum = 0;
        while(x > 0){
            sum += x % 10;
            x /= 10;
        }
        return sum;
    }

    private int getSum(int x, int y){
        return getSingle(x) + getSingle(y);
    }

}


活动打卡代码 AcWing 23. 矩阵中的路径

class Solution {
    private int direction[][] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    private int row;
    private int col;
    private boolean[][] visited;

    public boolean exist(char[][] board, String word) {
        if(board == null || word == null)
            throw new IllegalArgumentException("board or word cannot be null");
        row = board.length;
        if(row == 0)
            throw new IllegalArgumentException("board cannot be empty");
        col = board[0].length;
        if(col == 0)
            throw new IllegalArgumentException("board cannot be empty");
        visited = new boolean[row][col];
        for(int i = 0; i < row; i ++){
            for(int j = 0; j < col; j ++){
                if(searchWord(board, word, 0, i, j))
                    return true;
            }
        }
        return false;
    }

    private boolean searchWord(char[][] board, String word, int index, int startx, int starty){
        if(index == word.length() - 1){
            return board[startx][starty] == word.charAt(index);
        }
        if(board[startx][starty] == word.charAt(index)){
            visited[startx][starty] = true;
            for(int i = 0; i < 4; i ++){
                int newx = startx + direction[i][0];
                int newy = starty + direction[i][1];
                if(inArea(newx, newy) && !visited[newx][newy]){
                    if(searchWord(board, word, index + 1, newx, newy))
                        return true;
                }
            }
            visited[startx][starty] = false;
        }
        return false;
    }

    private boolean inArea(int x, int y){
        return x >= 0 && x < row && y >= 0 && y < col;
    }
}



class Solution {
    private int direction[][] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    private int row;
    private int col;
    private boolean[][] visited;

    public boolean exist(char[][] board, String word) {
        if(board == null || word == null)
            throw new IllegalArgumentException("board or word cannot be null");
        row = board.length;
        if(row == 0)
            throw new IllegalArgumentException("board cannot be empty");
        col = board[0].length;
        if(col == 0)
            throw new IllegalArgumentException("board cannot be empty");
        visited = new boolean[row][col];
        for(int i = 0; i < row; i ++){
            for(int j = 0; j < col; j ++){
                if(searchWord(board, word, 0, i, j))
                    return true;
            }
        }
        return false;
    }

    private boolean searchWord(char[][] board, String word, int index, int startx, int starty){
        if(index == word.length() - 1){
            return board[startx][starty] == word.charAt(index);
        }
        if(board[startx][starty] == word.charAt(index)){
            visited[startx][starty] = true;
            for(int i = 0; i < 4; i ++){
                int newx = startx + direction[i][0];
                int newy = starty + direction[i][1];
                if(inArea(newx, newy) && !visited[newx][newy]){
                    if(searchWord(board, word, index + 1, newx, newy))
                        return true;
                }
            }
            visited[startx][starty] = false;
        }
        return false;
    }

    private boolean inArea(int x, int y){
        return x >= 0 && x < row && y >= 0 && y < col;
    }
}



class Solution {
    public int findMin(int[] nums) {
        if(nums.length == 0) return 0;
        int n = nums.length;
        int left = 0, right = n - 1;
        while(left < right && nums[right] == nums[0]) right --;
        if(nums[left] <= nums[right]) return nums[0];
        while(left < right){
            int mid = left + right >> 1;
            if(nums[mid] < nums[0]) right = mid;
            else left = mid + 1;
        }
        return nums[left];
    }
}



class Solution {
    public int findMin(int[] nums) {
        if(nums.length == 0) return 0;
        int n = nums.length;
        int left = 0, right = n - 1;
        while(left < right && nums[right] == nums[0]) right --;
        if(nums[left] <= nums[right]) return nums[0];
        while(left < right){
            int mid = left + right >> 1;
            if(nums[mid] < nums[0]) right = mid;
            else left = mid + 1;
        }
        return nums[left];
    }
}


活动打卡代码 AcWing 21. 斐波那契数列

class Solution {
    public int fib(int n) {
        int a = 0, b = 1;
        while(n > 0){
            int c = a + b;
            a = b;
            b = c;
            n --;
        }
        return a;
    }
}



class Solution {
    public int fib(int n) {
        int a = 0, b = 1;
        while(n > 0){
            int c = a + b;
            a = b;
            b = c;
            n --;
        }
        return a;
    }
}