关于LeetCode 279的问题。Perfect Squares

时间:2019-01-01 01:30:19

标签: java algorithm breadth-first-search

给出一个正整数n,求和的最小平方数(例如1、4、9、16,...)。

示例1:

输入:n = 12 输出3 说明:12 = 4 + 4 +4。

示例2:

输入:n = 13 输出2 说明:13 = 4 + 9。

import java.util.*;

public class Solution {
    public int numSquares(int n) {
        int i = 1;
        int ret = Integer.MAX_VALUE;
        while(i * i < (n / 2)) {
            int start = i * i;
            int step = BFS(start,n);
            if(step < ret)
                ret = step;         
            i++;
      }

        return ret;
    }

    public int BFS(int start, int n) {
        int step = 0;
        LinkedList<Integer> queue = new LinkedList<Integer>();
        queue.offer(start);
        while(!queue.isEmpty()){
             int node = queue.poll();
             step++;
             if(node == n)
                 break;
             for(int k=1;k*k <= n;k++){
                 if(k*k <= n)
                 queue.offer(k * k + node); 
                 if(node + k*k == n)
                     return step + 1;
             }          
        }
        return step;
    }


}

我在更新步长值时遇到问题。我无法提出如何更新步长值的解决方案。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

恕我直言,我将尝试通过在step过程中检测不同marker之间的一些levels来更新BFS的值,谁可以告诉我们我们正在上一个级别的末尾,并进入下一个级别。我通常将null元素用作此marker

查看我的代码并附带注释,该注释已被接受

public class Solution {
    public int numSquares(int n) {
        Queue<Integer> queue=new LinkedList();
        queue.add(n);
        queue.add(null);//a null element to show the gap between two levels,i.e., the step
        int res=1;
        while(true){
            Integer i=queue.element();

            if(queue.size()!=1&&i==null){// we are hitting the marker
                queue.remove();
                res++;//i.e., step+1
                queue.add(null);//think about why we need add an additional marker here.
            }else{
                Integer sqrt=(int)Math.round(Math.sqrt(i));
                if(sqrt*sqrt==i){
                    break;
                }else{
                    queue.remove();
                    for(int j=sqrt;1<=j;j--){
                        if(0<i-j*j)
                            queue.add(i-j*j);
                    }
                }
            }
        }
        return res;
    }
}

PS,如上所述:

  

为方便起见,大多数标准库头文件已自动包含在内。

所以我们不做import java.util.*,并且代码可以编译。

相关问题