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();
*/