当前位置:网站首页>剑指offer基础版 ----- 第25天
剑指offer基础版 ----- 第25天
2022-07-31 05:09:00 【米兰的小红黑】

class Solution {
public int[] spiralOrder(int[][] matrix) {
if(matrix.length == 0){
return new int[0];
}
int[] arr = new int[matrix.length * matrix[0].length];
List<Integer> list= new ArrayList<>();
int left = 0;
int right = matrix[0].length -1;
int high = 0;
int low = matrix.length -1;
while(true){
int a = left;
while(a <= right){
list.add(matrix[high][a++]);
}
if(++high > low){
break;
}
int b = high;
while(b <= low){
list.add(matrix[b++][right]);
}
if(--right < left){
break;
}
int c = right;
while(c >= left){
list.add(matrix[low][c--]);
}
if(--low < high){
break;
}
int d = low;
while(d >= high){
list.add(matrix[d--][left]);
}
if(++left > right){
break;
}
}
int j = 0;
for(int i : list){
arr[j] = i;
j++;
}
return arr;
}
}

class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int i = 0;
for(int num : pushed) {
stack.push(num); // num 入栈
while(!stack.isEmpty() && stack.peek() == popped[i]) {
// 循环判断与出栈
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
边栏推荐
猜你喜欢

Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric

Simple read operation of EasyExcel

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?

快速掌握并发编程 --- 基础篇

Mysql application cannot find my.ini file after installation

Multiple table query of sql statement

【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级

Numpy中np.meshgrid的简单用法示例

SQL行列转换
随机推荐
剑指offer专项突击版 ---- 第2天
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
MySQL优化之慢日志查询
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
mysql 的简单运用命令
【LeetCode-SQL每日一练】——2. 第二高的薪水
tf.keras.utils.get_file()
Redis的初识
pytorch中的一维、二维、三维卷积操作
Numpy中np.meshgrid的简单用法示例
运用flask框架发送短信验证码的流程及具体代码
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
对list集合进行分页,并将数据显示在页面中
Sql解析转换之JSqlParse完整介绍
MySQL forgot password
Temporal介绍
一文了解大厂的DDD领域驱动设计
110 MySQL interview questions and answers (continuously updated)
centos7安装mysql5.7步骤(图解版)