常见算法 - 返回最少个整数的平方和等于给定值的个数

给出一个正整数n,寻找最少的完全平方数,是他们的和为n。

(leetcode279)For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

思路:以图的思想,每两个数相差一个完全平方数就连接一条边,即求n到0的最短路径;另外,用set标志当前数是否判断过,防止大量的重复计算。用一个队列存储当前步数可到达的数字,依次全部出队计算,再进行下一步数

class Solution {
    public int numSquares(int n) {
        //以是否相差平方数建立图
		// set存放能够到达的数字,防止重复计算
		HashSet<Integer> set = new HashSet<Integer>();
		// 队列存放当前数字一步能够到达的数字
		Queue<Integer> queue = new LinkedList<Integer>();
		int step = 0;
		queue.offer(n);
		while (!queue.isEmpty()) {
			int size = queue.size();
			step++;
			for (int j = 0; j < size; j++) {
				int curr = queue.poll();
				if (!set.add(curr)) {
					continue;
				}
				for (int i = (int) Math.sqrt(curr);  i >= 0; i--) {
//					System.out.println(i);
					if (curr - i * i == 0) {
						return step;
					}
					queue.offer(curr - i * i);
				}
			}
		}
		return n;
    }
}

猜你喜欢

转载自blog.csdn.net/b9x__/article/details/80226396