java - 使用递归的多维度斜率
问题描述
那是多维数组:
{ 3 13 15 28 30} ,
{55 54 53 27 26} ,
{54 12 52 51 50} ,
{50 10 8 53 11} ,
输出应该是if num = 1 : 6
,因为从 [1][0] 到 [1][2] 它的 3 然后从 [2][2] 到 [2][3] 它的 2 所以它将是 6 ,num
它的斜率用户想要,斜率可以向右或向下,禁止使用任何类型的循环,只能递归,尝试了 8 个小时来解决它,但我很困惑,这是我的代码:
private static int longestSlope (int [][] mat, int num , int i , int j , int count , int temp,int oldi,int oldj,boolean found)
{
System.out.println("oldi " +oldi);
System.out.println("oldj " +oldj);
System.out.println("temp " +temp);
System.out.println("count "+count);
System.out.println("i "+i);
System.out.println("j "+j);
if(i == mat.length-1 && j == mat[0].length-1)
return count;
if(i != mat.length && j != mat[0].length)
{
if(j < mat[0].length-1 && mat[i][j] - num == mat[i][j+1] ) // keep j+
{
if(temp == 0)
{
found = true;
oldj = j;
oldi = i;
}
System.out.println("check2");
if(j == 0)
temp = longestSlope(mat,num,i,j+1,count,temp+1,oldi,oldj,found);
else
temp = longestSlope(mat,num,i,j+1,count,temp+1,oldi,oldj+1,found);
}
else if(i < mat.length-1 && mat[i][j] - num == mat[i+1][j])
{
if(temp == 0)
{
found = true;
oldj = j;
oldi = i;
}
temp = longestSlope(mat,num,i+1,j,count,temp+1,oldi,oldj,found);
}
else if(j < mat[0].length-1)
temp = longestSlope(mat,num,i,j+1,count,temp,oldi,oldj,found);
else if(found)
{
System.out.println(found);
System.out.println("found i "+i);
System.out.println("found j "+j);
found = false;
if(j != mat[0].length-1)
temp = longestSlope(mat,num,oldi,oldj,count,0,oldi,oldj,found);
else if(j == mat[0].length-1 && temp != 0)
temp = longestSlope(mat,num,oldi,oldj,count,0,oldi,oldj,found);
else
temp = longestSlope(mat,num,oldi+1,0,count,0,oldi,oldj,found);
}
}
if(i < mat.length-1 && !found)
{
System.out.println("oldj222222222 "+oldj);
return longestSlope(mat,num,i+1,0,count,0,0,0,found);
}
return 0;
}
解决方案
推荐阅读
- android - 我的应用被拒绝,因为应用在应用之外有广告
- postgresql - 如何列出与其他表至少有一个关联的一张表中的记录?
- java - spring boot feign客户端集成测试配置
- android - 有什么方法可以在所有设备中使用 arcore 吗?
- node.js - 从 ffmpeg 通过 Janus 流式传输的视频不流畅
- python - Dlib 安装到本地 conda 目录而不是系统目录
- java - log4j.properties 文件在项目/类位置中消失
- python - 如何在python中删除数组的最后一列
- fpga - 在启动 DMA 和 RDMA 操作时没有 CPU 交互
- azure - 无法更新计划 - 启动-停止-VM