当前位置:网站首页>279. perfect square

279. perfect square

2022-06-12 18:17:00 qq_ forty-eight million two hundred and thirty thousand four hu

Give you an integer n , return And for n The minimum number of complete squares of .

Complete square It's an integer , Its value is equal to the square of another integer ; let me put it another way , Its value is equal to the product of the self multiplication of an integer . for example ,1、4、9 and 16 They're all perfect squares , and 3 and 11 No .

-------------------------------------------------------------------------------------------------------------------

Using dynamic planning and dual thinking for Cycle to solve 、

class Solution {

    public int numSquares(int n) {

        int[]res=new int[n+1];

        for(int i=1;i<=n;i++){

            int minn=Integer.MAX_VALUE;

            for(int j=1;j*j<=i;j++){

                minn=Math.min(minn,res[i-j*j]);

            }

            res[i]=minn+1;

        }

        return res[n];

    }

}

原网站

版权声明
本文为[qq_ forty-eight million two hundred and thirty thousand four hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281623269013.html