leetcode笔记: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

1^2 + 9^2 = 82 (各个位的平方和)

8^2 + 2^2 = 68

6^2 + 8^2 = 100

1^2 + 0^2 + 0^2 = 1

二. 题目分析

题目要求对任意一个正整数,不断各个数位上数字的平方和,若经过若干次运算后结果收敛到1,则该数字为Happy Number,不是Happy Number的数在经过多次运算后会从某个数开始陷入循环。这道题目我们只用根据规则进行计算,并使用一个map存储已经出现过的数字,这样每轮计算完成后查找map,若发现值已存在,证明已陷入循环,可跳出循环并判定该整数不是Happy Number。

更多关于Happy Number的知识可参考:http://baike.baidu.com/link?url=slZGeshN-Igmd4geJ7PZ9hPwk_PZGZK6QttvzH5TpUiQIPy8qZAmG6o9-5-x-Eu2WGoQuhnASB7alb2ecEatpVj0C9-3DaQPjy0Cpvmvp7AB3IiKy6vu1Qb8FhCbj_Eg1Nt4ba9FDzZT1BzbQcsL5a

三. 示例代码

#include <iostream>
#include <map>

using namespace std;

class Solution
{
public:
    bool isHappy(int n)
    {
        if (n < 0) return false;
        if (n == 1) return true;

        map<int, bool> showNum;

        while (true)
        {
            int temp = 0;
            while (n)
            {
                temp += (n % 10) * (n % 10);
                n /= 10;
            }
            if (temp == 1)
                return true;
            else if (showNum[temp] == true) // 陷入循环,判为非快乐数
                return false;
            else
            {
                showNum[temp] = true;
                n = temp;
            }
        }
    }
};

一些测试结果:

四. 小结

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 00:31:39

leetcode笔记:Happy Number的相关文章

LeetCode笔记--202Happy Number

1 题目描述 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 un

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

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

【LeetCode】Single Number

原文: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number 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

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you i