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 digits,
 6 and repeat the process until the number equals 1 (where it will stay),
 7 or it loops endlessly in a cycle which does not include 1.
 8 Those numbers for which this process ends in 1 are happy numbers.
 9  */
10
11 public class Solution {
12     /*
13      * solution:
14      * 1
15      * 2-4-16-37-58-89-145-42-20-4
16      * 3-9-81-65-61
17      * 4
18      * 5-25-29-85-89
19      * 6-36-45-41-17-50
20      * 7-49-97-130-10
21      * 8-64-52-29
22      * 9-81-65
23      * so only 1 and 7 is "happy"
24      */
25     public static boolean isHappy(int n) {
26         while(n/10>0){
27             int sum=0;
28             while(n/10>0){
29                 sum+=Math.pow((n%10),2);
30                 n=n/10;
31             }
32             sum+=Math.pow(n,2);
33             n=sum;
34         }
35         if (n==1||n==7)
36             return true;
37         return false;
38     }
39     public static void main(String[] args) {
40         // TODO Auto-generated method stub
41         System.out.println(isHappy(23456));
42     }
43 }
时间: 2024-12-11 11:51:39

LeetCode----202. Happy Number(Java)的相关文章

Leetcode 202. Happy Number

202. Happy Number Total Accepted: 78171 Total Submissions: 208635 Difficulty: Easy 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 t

Java for LeetCode 202 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

leetcode 202. Happy Number 判断一个数是否是“Happy Number” ---------- java

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

40. leetcode 202. 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

Leetcode 263. Ugly Number JAVA语言

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

[LeetCode] 136.Single Number Java

题目:Given an array of integers, every element appears twice except for one. Find that single one. 题意及分析:一个数组中,有一个数只出现了一次,其他的出现了两次.要求给出只出现一次的数.这道题,最简单的方法是用一个hashtable来储存,然后得出结果,但是效率比较低.也可以用位运算求解.直接使用异或运算,因为对于异或运算有:n^0=n,n^n=0,所以相同的数异或为0,最后得出的结果就为出现一次的数

[LeetCode] 179. Largest Number Java

题目: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of

LeetCode 202 Happy Number(开心数)(vector、unordered_set)

翻译 写一个算法来决定一个数是否是"开心"的. 开心数是被如下步骤所定义的数: 从所有正整数开始,用其每个数字的平方和来替代这个数,不断重复这个过程直到最后的结果为1(此时它就停止了), 或者它在一个不包含1的周期内无限循环. 这些在这个过程中以1结尾的数就是开心数. 例如:19是开心数. 12+92=82 82+22=68 62+82=100 12+02+02=1 原文 Write an algorithm to determine if a number is "happ

LeetCode 009 Palindrome Number - Java

Determine whether an integer is a palindrome. Do this without extra space. 定位:简单题 题目要求判断给出的数字是否是回文数,并且要求不适用额外空间.我们不能使用其他数据类型过度处理,那从个位开始计算,每提高计算一位将原先的值*10+新值,结束后判断是否与原值相同即可. Java实现: 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 in

Leetcode 202 Happy Number 弗洛伊德判环

弗洛伊德判环! 1 class Solution { 2 public: 3 int change(int n){ 4 int ans = 0; 5 for ( ; n!=0 ; ans+= (n%10)*(n%10),n/=10); 6 return ans; 7 } 8 bool isHappy(int n) { 9 int one = n; 10 int two = n; 11 while(1){ 12 one = change(one); 13 two = change(change(t