当前位置:网站首页>Code simulation
Code simulation
2022-07-21 17:50:00 【Invincible rookie ya】
Print matrix clockwise
public class Solution {
public static ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> res=new ArrayList<>();
int m=matrix.length,n=matrix[0].length;
int[][] flag=new int[m][n];
int[][] direction={
{0,1},{1,0},{0,-1},{-1,0}};
if(m==0 || n==0) return res;
int i=0,j=0,k=0,time=0;
dfs(matrix,res,i,j,m,n,k,direction,flag,time);
return res;
}
public static void dfs(int[][] matrix,ArrayList<Integer> res,int i,int j,int m,int n,int k,int[][] direction,int[][] flag,int time) {
System.out.println(matrix[i][j]);
res.add(matrix[i][j]);
flag[i][j] = 1;
time+=1;
int nexti = i + direction[k % direction.length][0];
int nextj = j + direction[k % direction.length][1];
if(time>=m*n) return; //time It can be used to judge the end condition
while (nexti < 0 || nexti >= m || nextj < 0 || nextj >= n || flag[nexti][nextj]==1) {
int index = (++k) % direction.length;
nexti = i + direction[index][0];
nextj = j + direction[index][1];
}
dfs(matrix, res, nexti, nextj, m, n, k, direction, flag,time);
}
}
public class Solution {
static ArrayList<Integer> res = new ArrayList<>();
public static ArrayList<Integer> printMatrix(int [][] matrix) {
if(matrix==null||matrix.length==0) return res;
int m = matrix.length;
int n = matrix[0].length;
print(0, n-1, 0, m-1, matrix);
return res;
}
public static void print(int left, int right, int top, int bottom, int [][] matrix){
// From left to right
for(int i = left; i <= right; i ++) res.add(matrix[top][i]);
// The next layer
top ++;
if(left > right || top > bottom) return;
// From top to bottom
for(int i = top; i <= bottom; i ++) res.add(matrix[i][right]);
right --;
if(left > right || top > bottom) return;
// From right to left
for(int i = right; i >= left; i --) res.add(matrix[bottom][i]);
bottom --;
if(left > right || top > bottom) return;
// From bottom to top
for(int i = bottom; i >= top; i --) res.add(matrix[i][left]);
left ++;
// recursive
print(left, right, top, bottom, matrix);
}
}
Judge whether playing cards are shunzi
public class Solution {
public static boolean IsContinuous(int [] numbers) {
Arrays.sort(numbers);
int len=numbers.length;
int count=0,zeronum=Integer.MAX_VALUE;
for(int i=0;i<len-1;++i){
if(numbers[i]==numbers[i+1])
return false;
if(i>=zeronum) count+=(numbers[i+1]-numbers[i]); // Calculate the deviation and
if(numbers[i]==0 && numbers[i+1]!=0) zeronum=i; //0 The number of
}
if(count<=zeronum) return true; // Judge
else return false;
}
}
Convert a string to an integer
public class Solution {
public static int StrToInt (String s) {
if(s==null && s.length()==0) return 0;
String string=s.trim();
if(string.length()==0) return 0;
boolean flag=true;
char numone=string.charAt(0);
long res=0; // Notice that this is Long Ah ah ah
if(!num(numone) && numone!='-' && numone!='+') return 0;
if(numone=='-') flag=false;
else if(numone=='+') flag=true;
else res=numone-'0';
for(int i=1;i<string.length();++i){
char c=string.charAt(i);
if(!num(c))
break;
else{
res=res*10+(c-'0'); // Otherwise, it will calculate according to overflow , The latter judgment doesn't work at all
if(flag && res>Integer.MAX_VALUE) return Integer.MAX_VALUE;
if(!flag && res*(-1)<Integer.MIN_VALUE) return Integer.MIN_VALUE;
}
}
if(flag) return (int) res;
else return (int) -res;
}
public static boolean num(char c){
if(c>='0' && c<='9')
return true;
else return false;
}
}
To be continued !
边栏推荐
- 国外LEAD行业到底什么情况?心态崩溃直到放弃
- Thread synchronization and interlock of C (42)
- Code之位运算
- [CTF]-NepCTF2022
- Design of online blog system
- Practical guidance of interface automation testing (Part 2): idea of setting assertion of interface automation testing
- YII框架安装步骤(yii框架版本1.1.20,时间是2018/11)
- 浅聊一波moveit2
- 王者荣耀商城异地多活部署设计
- 中国高校江湖绰号
猜你喜欢
Jmeter-正则、xpath、JSON
传输层 使用Web代理访问网站
C#(三十七)之基于流的文件操作(FileStream)
C (36) folder, path, environment special directory class
How to use measurement data to drive the improvement of code review
Methods to effectively prevent overflow and underflow during softmax calculation
Advanced audio and video development | Lecture 4: audio automatic gain control AGC
Hands on moveit2 | introduction and installation
212. 单词搜索 II
How to extract coordinate information in MATLAB visual image window figure
随机推荐
ipset v7.10: Kernel error received: set type not supported
C (37) FileStream
C#(三十六)之文件夹、路径、环境特殊目录类
DeFi 对经济模式带来的改变和存在的局限
ROS2入门教程 | 动作(Action)通信与自定义接口
Kubernetes APIServer,Etcd,controller manager,scheduler 高可用原理
无码时代,企业数字化转型该如何发展?
Some functions of C (39) about string
解决Metasploit中shell乱码的问题
Yarn run dev cannot load file c:\program files\nodejs\node_ global\yarn. PS1, because script execution is prohibited in this system
详解缓存穿透、缓存雪崩、缓存击穿
ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
【pinia源码】二、defineStore源码解析
家园防线 | 全栈物联网灌溉系统技术指南
Meaning of downstream task
勤于奋分享国外LEAD任务了
C#(四十五)之线程池
JMeter regular, XPath, JSON
Encapsulate the global input component and bind parent-child data through V-model
免费、强大的开源笔记软件Joplin综合评测 —印象笔记的开源替代