力扣1179. 重新格式化部门表
难度:简单
内容:行转列、group by
部门表 Department:
Column Name | Type |
---|---|
id | int |
revenue | int |
month | varchar |
(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值
[“Jan”,“Feb”,“Mar”,“Apr”,“May”,“Jun”,“Jul”,“Aug”,“Sep”,“Oct”,“Nov”,“Dec”]。
–
编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。
查询结果格式如下面的示例所示:
Department 表:
id | revenue | month |
---|---|---|
1 | 8000 | Jan |
2 | 9000 | Jan |
3 | 10000 | Feb |
1 | 7000 | Feb |
1 | 6000 | Mar |
查询得到的结果表:
id | Jan_Revenue | Feb_Revenue | Mar_Revenue | … | Dec_Revenue |
---|---|---|---|---|---|
1 | 8000 | 7000 | 6000 | … | null |
2 | 9000 | null | null | … | null |
3 | null | 10000 | null | … | null |
注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。
解答:
select id,
max(if(month='Jan',revenue,null)) as Jan_Revenue,
max(if(month='Feb',revenue,null)) as Feb_Revenue,
max(if(month='Mar',revenue,null)) as Mar_Revenue,
max(if(month='Apr',revenue,null)) as Apr_Revenue,
max(if(month='May',revenue,null)) as May_Revenue,
max(if(month='Jun',revenue,null)) as Jun_Revenue,
max(if(month='Jul',revenue,null)) as Jul_Revenue,
max(if(month='Aug',revenue,null)) as Aug_Revenue,
max(if(month='Sep',revenue,null)) as Sep_Revenue,
max(if(month='Oct',revenue,null)) as Oct_Revenue,
max(if(month='Nov',revenue,null)) as Nov_Revenue,
max(if(month='Dec',revenue,null)) as Dec_Revenue
from Department
group by id;
执行用时:350ms - 97.77%
力扣1004. 最大连续1的个数 III
难度:中等
内容:双指针、滑动窗口Sliding Window
给定一个由若干 0 和 1 组成的数组 A,我们最多可以将 K 个值从 0 变成 1 。
返回仅包含 1 的最长(连续)子数组的长度。
示例 1:
输入:A = [1,1,1,0,0,0,1,1,1,1,0], K = 2
输出:6
解释:
[1,1,1,0,0,1,1,1,1,1,1]
粗体数字从 0 翻转到 1,最长的子数组长度为 6。
–
示例 2:
输入:A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3
输出:10
解释:
[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
粗体数字从 0 翻转到 1,最长的子数组长度为 10。
–
提示:
1 <= A.length <= 20000
0 <= K <= A.length
A[i] 为 0 或 1
解答:
【参考】https://leetcode-cn.com/problems/max-consecutive-ones-iii/comments/577575
class Solution {
public int longestOnes(int[] A, int K) {
int res =0,i=0,j=0;
while(j<A.length){
if(A[j]==0){
if(K==0){
while(A[i]==1) i++; //直到左指针指向0时会停止
i++; //因为k已无份额 需往后跳一个0
}else{
K--; //A[j]==0才k--
}
}
res = res > ++j - i? res : j - i;
// i左指针 j右指针
}
return res;
}
}
执行用时:3ms - 90.86%
备注:
9/5/20的bilibili笔试编程题第一题,我当时思路不是特别流畅,用了一个数组保存0的下标,然后直接在这个数组的前提下计算。没有捋清楚时容易出现很多边界问题,包括容易漏1,结果ac了50%就没有再推进了。加油积累。
力扣54. 螺旋矩阵
难度:中等
内容:数组
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
–
示例 2:
输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
解答:
【参考】https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-zuo-dao-you-shang-dao-xia-you-dao/
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix.length == 0) return res;
int m = matrix.length, n = matrix[0].length;
int u = 0, d = m - 1, l = 0, r = n - 1;
while (true) {
//左到右
for (int j = l; j <= r; j++) {
res.add(matrix[u][j]);
}
if (++u > d) break;
//上到下
for (int i = u; i <= d; i++) {
res.add(matrix[i][r]);
}
if (l > --r) break;
//右到左
for (int j = r; j >= l; j--) {
res.add(matrix[d][j]);
}
if (u > --d) break;
//下到上
for (int i = d; i >= u; i--)
res.add(matrix[i][l]);
if (++l > r) break;
}
return res;
}
}
备注:
9/5/20的bilibili笔试编程题第二题,这题当时在纸上演算了挺久,隐约有点方向但也没法形成通式,熬到后来只好写了一些好处理的特殊用例,印象里还勉强ac了15%左右。深刻体会到,积累太少的状态就是这样了,说不上很难,但是如果没见过也很难说有个相对明确的方向。就算是见过,甚至是写过多次的都不能保证满分ac呢,学习的路还很长,不能气馁不能退缩要振作要迈开步加油加油加油!!
https://blog.csdn.net/Purpose1123/article/details/88770390?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.channel_param
9/5/20的bilibili笔试第三题