202. Happy Number(LeetCode)

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 class Solution {
     2 public:
     3    int fang(int n)
     4     {
     5         int sum = 0;
     6         while (n != 0)
     7         {
     8             sum += (n % 10)*(n % 10);
     9             n = n / 10;
    10         }
    11         cout<<sum<<endl;
    12         return sum;
    13     }
    14
    15     bool isHappy(int n) {
    16         int i=0;
    17             while (n!=1&&i<100)
    18             {
    19                 n = fang(n);
    20                 cout<<n<<endl;
    21                 i++;
    22             }
    23             if (n == 1&&i<100)
    24             {
    25                 return true;
    26             }
    27             if (i >= 100)
    28             return false;
    29     }
    30 };
     1 class Solution {
     2 public:
     3     bool isHappy(int n) {
     4         unordered_map<int, bool> m;
     5         while (n != 1 && !m.count(n)){
     6             m[n] = true;
     7             int new_n = 0;
     8             while (n){
     9                 new_n += (n % 10)*(n % 10);
    10                 n /= 10;
    11             }
    12             n = new_n;
    13         }
    14         return n == 1;
    15     }
    16 };
时间: 2024-10-13 14:24:06

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

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"

不邻接植花(leetcode)

有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或者离开. 你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同. 以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类.花的种类用  1, 2, 3, 4 表示.保证存在答案. 示例 1: 输入:N = 3,

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

JS中六种数据类型(四)——Number (转)

Number类型应该是ECMAScript中最令人关注的数据类型了,这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被称为双精度数值).为支持各种数值类型,ECMA-262定义了不同的数值字面量. 最基本的数值字面量格式是十进制整数,十进制整数可以像下面这样直接在代码中输入: var  item =55;   //整数 除了以十进制表示外,整数还可以通过八进制(以8为基数)或十六进制(以16为基数)的字面值来表示.其中,八进制字面值的第一位必须是零(0),然后是八进制数