LeetCode刷题

1 数组

1.1 二维数组

1.1.1 三维形体投影面积

题目链接:https://leetcode-cn.com/problems/projection-area-of-3d-shapes/

class Solution {
    public int projectionArea(int[][] grid) {
        int sBottom = 0;
        int xLen = grid.length;
        int yLen = grid[0].length;
        
        //三个不同平面的投影
        int xTotal = 0;
        int yTotal = 0;
        int zTotal = 0;//底部投影面积

        for (int i = 0; i<xLen; i++) {
            
            int xMax = grid[i][0];
            int yMax = grid[0][i];
            for (int j = 0; j < yLen; j++) {
                //底部投影面积
                if (grid[i][j] > 0) {
                   zTotal ++; 
                }
                
                //每一行和每一列的最大值
                xMax = Math.max(xMax, grid[i][j]);
                yMax = Math.max(yMax, grid[j][i]);
            }
            xTotal = xTotal + xMax;
            yTotal = yTotal + yMax; 


        }
        return (xTotal + yTotal + zTotal);

    }

1.1.2 二维网格迁移

题目链接:https://leetcode-cn.com/problems/shift-2d-grid/

class Solution {
    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        List res = new ArrayList<>();
        int m = grid.length;
        if (m == 0) return res;
        int n = grid[0].length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ans[(i + (j + k) / n) % m][(j + k) % n] = grid[i][j];
            }
        }
        res = Arrays.asList(ans);
        return res;
    }
}

1.1.3 航班预订统计

题目链接:https://leetcode-cn.com/problems/corporate-flight-bookings

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) { 
        int[] flightArr = new int[n];
        for (int i = 0; i < bookings.length; i++) {
            for (int j = bookings[i][0]; j <= bookings[i][1]; j++) {
                flightArr[j - 1] += bookings[i][2];
            }
        }

        return flightArr;
    }
}

 

2、Collection

2.1 Stack

2.1.1 包含min函数的栈

https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/

class MinStack {
   Stack<Integer> staData;
   Stack<Integer> staMin;

    /** initialize your data structure here. */
    public MinStack() {
        staData = new Stack<Integer>();
        staMin = new Stack<Integer>();
    }
    
    public void push(int x) {
        staData.push(x);
        if(staMin.empty() || staMin.peek() >= x)
        {
            staMin.push(x);
        }
    }
    
    public void pop() {
        int popVal = staData.pop();
         if (popVal == staMin.peek())
         {
            staMin.pop();
         }
    }
    
    public int top() {
        return staData.peek();
    }
    
    public int min() {
        return staMin.peek();
   
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>