本文共 534 字,大约阅读时间需要 1 分钟。
剑指Offer做过一次,奈何没充分理解,又忘记了。思路:之前上下左右都走,写完看别人写的发现只需向右向下就可以了。
(i/0+i%0+j/0+j%10)>k跳出。class Solution { public int movingCount(int m, int n, int k) { boolean visited[][]= new boolean[m][n]; return rec(0,0,m,n,k,visited); } public int rec(int i,int j,int m,int n,int k,boolean[][] visited){ if(i>=m||j>=n||(i/10+i%10+j/10+j%10)>k||visited[i][j]){ return 0; } visited[i][j]=true; return rec(i+1,j,m,n,k,visited)+rec(i,j+1,m,n,k,visited)+1; }}
转载地址:http://jsnwi.baihongyu.com/