当前位置:网站首页>Code之模拟
Code之模拟
2022-07-20 23:58:00 【无敌菜鸟ya】
顺时针打印矩阵
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可用作判断结束条件
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){
// 从左到右
for(int i = left; i <= right; i ++) res.add(matrix[top][i]);
// 下一层
top ++;
if(left > right || top > bottom) return;
// 从上到下
for(int i = top; i <= bottom; i ++) res.add(matrix[i][right]);
right --;
if(left > right || top > bottom) return;
// 从右到左
for(int i = right; i >= left; i --) res.add(matrix[bottom][i]);
bottom --;
if(left > right || top > bottom) return;
// 从下到上
for(int i = bottom; i >= top; i --) res.add(matrix[i][left]);
left ++;
// 递归
print(left, right, top, bottom, matrix);
}
}
判断扑克牌中是否是顺子
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]); //计算偏差和
if(numbers[i]==0 && numbers[i+1]!=0) zeronum=i; //0的个数
}
if(count<=zeronum) return true; //判断
else return false;
}
}
把字符串转为整数
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; //注意这里是Long啊啊啊啊
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'); //否则在这里它自己就按照溢出计算,后面的判断根本不起效
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;
}
}
未完待续!
边栏推荐
- 知识图谱赋能数字经济,第十六届全国知识图谱与语义计算大会(CCKS 2022)即将召开
- How to use measurement data to drive the improvement of code review
- 第2讲 Hi3861的WiFi实验-Station模式
- XFS opens a new way of e-commerce
- Methods to effectively prevent overflow and underflow during softmax calculation
- YOLOv7实验测试之一:遥感图像检测应用
- JMeter regular, XPath, JSON
- 同城两中心自适应同步模式部署
- DeFi 对经济模式带来的改变和存在的局限
- 勤于奋分享国外LEAD任务了
猜你喜欢
C#(三十八)之StreamWriter StreamWriter使用方法及与FileStream类的区别
勤于奋分享国外LEAD任务了
Free and powerful open source note taking software Joplin comprehensive evaluation - Open Source substitution of impression notes
212. 单词搜索 II
YII框架安装步骤(yii框架版本1.1.20,时间是2018/11)
471-82(647、5、92、143、148、19)
Some functions of C (39) about string
C#(三十四)之坐标变换
Encapsulate the global input component and bind parent-child data through V-model
Yolov7 experiment test 1: remote sensing image detection application
随机推荐
ROS2入门教程 | 动作(Action)通信与自定义接口
C (37) FileStream
解决Metasploit中shell乱码的问题
212. 单词搜索 II
Traverse the pictures of folders and subfolders, use OpenCV to change the size and save to a folder
勤于奋分享国外LEAD任务了
【测试开发】软件测试——测试用例设计&测试分类详解
ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
Coordinate transformation of C (34)
6条shell小技巧,让脚本显得不再业余
Kruskal 重构树
ICML 2022 | tutorial validity, reliability and significance: a statistical method tutorial for reproducible machine learning
【2023提前批 之 面经】~ 锐捷
RuntimeError: CUDA out of memory. Tried to allocate 32.00 MIB | view GPU memory
免费、强大的开源笔记软件Joplin综合评测 —印象笔记的开源替代
知识图谱赋能数字经济,第十六届全国知识图谱与语义计算大会(CCKS 2022)即将召开
MySQL explain execution plan analysis
如何用Sketch设计网页,创建栅格辅助线教程
为啥有些经理,总是强行刷存在感?(你遇到过吗?)
Mutex mutex of thread C (43) is mutually exclusive