Happy Number

Tags: Hash Table, Math, Easy

Question

Problem Statement

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any
positive integer, replace the number by the sum of the squares of its digits,
and repeat the process until the number equals 1 (where it will stay), or it
loops endlessly in a cycle which does not include 1. Those numbers for which
this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • $$1^2 + 9^2 = 82$$
  • $$8^2 + 2^2 = 68$$
  • $$6^2 + 8^2 = 100$$
  • $$1^2 + 0^2 + 0^2 = 1$$

Credits:
Special thanks to @mithmatt and
@ts for adding this problem and
creating all test cases.

题解

根据指定运算规则判断输入整数是否为『happy number』,容易推断得知最终要么能求得1,要么为环形队列不断循环。
第一种情况容易判断,第二种情况即判断得到的数是否为环形队列,也就是说是否重复出现,这种场景使用哈希表轻易解决。

Java

  1. public class Solution {
  2. public boolean isHappy(int n) {
  3. if (n < 0) return false;
  4. Set<Integer> set = new HashSet<Integer>();
  5. set.add(n);
  6. while (n != 1) {
  7. n = digitsSquareSum(n);
  8. if (n == 1) {
  9. return true;
  10. } else if (set.contains(n)) {
  11. return false;
  12. } else {
  13. set.add(n);
  14. }
  15. }
  16. return true;
  17. }
  18. private int digitsSquareSum(int n) {
  19. int sum = 0;
  20. for (; n > 0; n /= 10) {
  21. sum += (n % 10) * (n % 10);
  22. }
  23. return sum;
  24. }
  25. }

源码分析

辅助方法计算数字平方和。

复杂度分析

有限迭代次数一定终止,时间和空间复杂度均为 O(1).