zoukankan      html  css  js  c++  java
  • Leetcode: Happy Number

    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

    First Time: Not good, when I try to get to the most significant bit, I define current bit called cur, and cur *= 10 each time. This might gives rise to overflow in line 12 when cur * 10. To overcome this I have to define cur to be a long.

    The best way to get each digit is to use n%10, and n /=10 each loop. Dividing will avoid unnecessary overflow.

    BTW, no need to define an arraylist to store all the digits.

     1 public class Solution {
     2     public boolean isHappy(int n) {
     3         if (n <= 0) return false;
     4         if (n == 1) return true;
     5         HashSet<Integer> set = new HashSet<Integer>();
     6         while(!set.contains(n)) {
     7             set.add(n);
     8             long cur = 1;
     9             ArrayList<Integer> digits = new ArrayList<Integer>();
    10             int sum = 0;
    11             while (n / cur >= 1) {
    12                 digits.add((int)(n % (cur*10) / cur));
    13                 cur *= 10;
    14             }
    15             for (int digit : digits) {
    16                 sum += (int)Math.pow(digit, 2);
    17             }
    18             n = sum;
    19             if (n == 1) return true;
    20         }
    21         return false;
    22     }
    23 }

    About syntax, line 16 it is correct to write it as: sum += Math.pow(digit, 2),

    but it is incorrect as: sum = sum + Math.pow(digit, 2), should write as sum = sum + (int)Math.pow(digit, 2)

    So better cast pow into integer before use it.

    Second Time: Better

    The best way to get each digit is to use n%10, and n /=10 each loop. 

     1 public class Solution {
     2     public boolean isHappy(int n) {
     3         if (n < 0) return false;
     4         if (n == 1) return true;
     5         HashSet<Integer> set = new HashSet<Integer>();
     6         while (n != 1) {
     7             if (set.contains(n)) return false;
     8             set.add(n);
     9             int sum = 0;
    10             while (n > 0) {
    11                 sum += (int)Math.pow(n%10, 2);
    12                 n /= 10;
    13             }
    14             n = sum;
    15         }
    16         return true;
    17     }
    18 }
  • 相关阅读:
    java语言实现堆排序
    堆排序
    静态分派
    谈mysql优化
    Unity2017.1官方UGUI文档翻译——Canvas
    Unity2017.1官方UGUI文档翻译——Rect Transform
    Unity2017.1官方UGUI文档翻译——Rich Text
    Unity2017.1官方UGUI文档翻译——Auto Layout
    Unity2017.1官方UGUI文档翻译——Animation Integration
    Unity2017.1官方UGUI文档翻译——Interaction Components
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5046973.html
Copyright © 2011-2022 走看看