1. 首页
  2. 软件开发
  3. day-26 查询网格图中每一列的宽度

day-26 查询网格图中每一列的宽度

  • 发布于 2024-09-26
  • 1 次阅读


思路:
利用两个for循环,外层for用于遍历列数,内层for用于计算每一列的宽度

解题方法:
内层for循环时:
1.当前所在位置的值为0 则宽度为1
2.当前所在位置的值大于0 则宽度通过不断取商得到
3.当前所在位置的值大于0 则宽度在情况2的基础上再加1

code:

class Solution {

    public int[] findColumnWidth(int[][] grid) {

        int n=grid[0].length;
        int m=grid.length;
        int ans[]=new int[n];
        for(int i=0;i<n;i++){

            int length=0;
            for(int j=0;j<m;j++){

                int len=0;
                int num=grid[j][i];
                if(num==0)len=1;
                if(num<0)len=1;
                while(num!=0){

                    len++;
                    num/=10;
                }
                length=Math.max(length,len);
            }
            ans[i]=length;
        }
        return ans;
    }
}

原文链接: https://blog.csdn.net/qq_53568730/article/details/138267917