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 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,则最原始的数就是一个”Happy Number”。如果在还没达到1的过程中出现循环,则这个数肯定不是。用到C++ 的set。set.count()判断一个数是否在set中,set.insert(),向set 中插入元素。INT_MAX,表示最大整数。

时间: 2024-10-26 08:23:02

40. leetcode 202. Happy Number的相关文章

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

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

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

LeetCode:Happy Number - 快乐数

1.题目名称 Happy Number(快乐数) 2.题目地址 https://leetcode.com/problems/happy-number/ 3.题目内容 英文: 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, repla

202. Happy Number【leetcode】java,hashSet,算法

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

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen