Happy Problem【leetcode202】

Tips:

第一次没有AC是因为没有判断1这个特殊的数,其他没有什么太难的地方,代码和注释如下:

public class Solution202 {
    public boolean isHappy(int n) {
        int nn = n,temp = 0;
        Set<Integer> set = new HashSet<Integer>();<span style="white-space:pre">	</span>//用set检测是否回到了起点
        set.add(n);<span style="white-space:pre">					</span>//将n加入
        if(n == 1)<span style="white-space:pre">					</span>//1需要先判断
            return true;
        while(temp!=1) {
            temp = 0;
            while (nn != 0) {<span style="white-space:pre">				</span>//各位的平方和
                temp += Math.pow(nn % 10, 2);
                nn = nn / 10;
            }
            if(set.contains(temp))<span style="white-space:pre">			</span>
                return false;
            set.add(temp);
            nn = temp;
        }
        return true;<span style="white-space:pre">					</span>//若循环结束则是Happy Number
    }
}
时间: 2024-12-28 17:36:34

Happy Problem【leetcode202】的相关文章

hdu 5371 Hotaru&#39;s problem【manacher】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意: 给出一个长度为n的串,要求找出一条最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第二段回文.求最大子串的长度. 代码: #include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<ctype.h&

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

HDU3199 Hamming Problem 【数论】

Hamming Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 703    Accepted Submission(s): 289 Problem Description For each three prime numbers p1, p2 and p3, let's define Hamming sequence

HDOJ-1016 Prime Ring Problem【DFS】

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47217    Accepted Submission(s): 20859 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 1032 The 3n + 1 problem【递归】

/* 中文题目 3n+1问题 中文翻译-大意 当一个数n是奇数的时候变成3*n+1,为偶数的时候变成n/2 解题思路:区间内每个数逐个求解 难点详解:如何逐个计算每个数的次数,选用while循环,还有就是将此时的 i 值赋给data,用于while循环的条件.最后再将这一个数运算次数累加在一起 关键点:理解题意 解题人:lingnichong 解题时间:2014-06-03 09:40:39 解题体会:没有告诉你那个大那个小,要先比较一下数,是个陷阱 */ The 3n + 1 problem

HDOJ3374 String Problem 【KMP】+【最小表示法】

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1512    Accepted Submission(s): 668 Problem Description Give you a string with length N, you can generate N strings by left shifts

HDU2132 An easy problem 【Java】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9194    Accepted Submission(s): 2491 Problem Description We once did a lot of recursional problem . I think some of them is easy