[LeetCode202] 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

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 =
    1

Solution:

use loop to calcuate the square sum until meet one of these two stops:

1. sum is 1, return true

2. sum value got before, which means there is a sum loop and will never goes to 1. return false

public boolean isHappy(int n) {
        if(n<=0) return false;
        Set<Long> records = new HashSet<>();
        long ln = n;
        while(ln< Integer.MAX_VALUE){
            if(records.contains(ln)) return false;
            else records.add(ln);
            ln = getSquareSum(ln);
            if(ln == 1) return true;
        }
        return false;
    }
    public long getSquareSum(long n){
        long sum = 0;
        while(n!=0){
            sum+= Math.pow(n%10,2);
            n/=10;
        }
        return sum;
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 06:04:37

[LeetCode202] Happy Number的相关文章

LeetCode----202. Happy Number(Java)

1 package isHappy202; 2 /* 3 * Write an algorithm to determine if a number is "happy". 4 A happy number is a number defined by the following process: 5 Starting with any positive integer, replace the number by the sum of the squares of its digit

LeetCode202:Happy Number 。C#版,在vs2010中通过,leetcode中Wrong Answer

static List<int> nums = new List<int>(); public static bool IsHappy(int n) { int newint = 0; while (n != 0) { newint += ((n % 10) * (n % 10)); n = n / 10; } if (newint == 1) return true; if (nums.Contains(newint)) { return false; } else { nums

Floyd判圈算法(判断链表是否含环)

Floyd判圈算法 简介 Floyd判圈算法,也称龟兔赛跑算法,可用于判断链表.迭代函数.有限状态机是否有环.如果有,找出环的起点和大小.时间复杂度O(n),空间复杂度O(1). 可以先思考一下,假设有一个圆形的跑道周长为\(C\),A和B从同一个起点,分别以\(v\)和\(2v\)的速度同向出发,可以知道,因为B比A跑得快而且跑道是环形的,所以接下来一定存在某一时刻,B和A相遇,这时候,B跑过的总距离\(S_B\)减去A跑的总距离\(S_A\)一定是\(C\)的整数倍.即: \(S_B-S_A

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of