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 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
 1 public class Solution {
 2     public boolean isHappy(int n) {
 3         int m = 0;
 4
 5         while (n > 9)
 6         {
 7             m += Math.pow( n%10,2 );
 8             n = n / 10;
 9         }
10         m += Math.pow( n,2 );
11
12         if(m > 9)
13         {
14             return isHappy(m);
15         }
16
17         if(m == 1)
18         {
19             return true;
20         }else{
21             return false;
22         }
23     }
24 }
时间: 2024-10-05 02:14:39

LeetCode 202的相关文章

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

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 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

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

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(开心数)(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#202] Roman to Integer

Problem: 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

[LeetCode] 202. 快乐数

题目链接:https://leetcode-cn.com/problems/happy-number/ 题目描述: 编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. 示例: 输入: 19 输出: true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82

leetcode——202.快乐数

别人好聪明,为什么我只能想到思路但是却实现不出来.... 还是得多做才会吧 class Solution: def isHappy(self, n: int) -> bool: n=str(n) v=set() while 1: n=str(sum(int(i)**2 for i in n)) if n=='1': return True if n in v: return False v.add(n) 执行用时 :32 ms, 在所有 Python3 提交中击败了100.00%的用户 内存消耗