【leetcode】happy number--easy

如果一个数的每个位的整数的平方和等于1,则为happy number

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1
#include<iostream>
#include<vector>
#include <math.h>
using namespace std;

class Solution {
public:
    bool isHappy(int n) {
        temp.insert(temp.begin(),n);   //把当前值加入到历史路径中
        int r=addsquare(n,0);
        if(r==1)   //如果已经达到了happy number的条件
            return true;
        else
        {
            for(vector<int>::iterator it=temp.begin();it!=temp.end();it++)    //如果没有达到happy number的条件,那么比较r值和历史值是否重合
            {
                if(r==*it)
                    return false;   //如果和历史值重合了,则不是happy number
            }
            isHappy(r);   //如果和历史结果没有重合,则继续往后计算
        }
    }
private:
    vector<int> temp;
    int addsquare(int n,int r){
            //char str[11];
            //itoa(n,str,10);    //数字转换为字符串,10代表10进制
            //int r=0;
            //char *ptr=str;
            //while(*ptr!=‘\0‘)
            //{
            //    r+=(*ptr-‘0‘)*(*ptr-‘0‘);
            //    ptr++;
            //}
            //return r;
        int tmp=n%10;
        r+=tmp*tmp;
        n=(n-tmp)/10;
        if(n==0)
            return r;
        else
            addsquare(n,r);
        }
};

void main()
{
    Solution S;
    int n;
    cin>>n;
    cout<<S.isHappy(n);
}
时间: 2024-10-16 10:59:44

【leetcode】happy number--easy的相关文章

【leetcode】Palindrome Number (easy)

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【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】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】Single Number I &amp; II

Single Number I : 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? Solution: 解法不少,贴一种: 1 cla

【LeetCode】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 return a string instead of

【LeetCode】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 statement to be ambig

【leetcode】Happy Number(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 the number by the sum of the squares of its digits, and repeat the process until the

【Leetcode】Ugly Number

题目链接:https://leetcode.com/problems/ugly-number/ 题目: 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 i

【Leetcode】Guess Number Higher or Lower II

题目链接: 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a

【Leetcode】Happy Number

题目链接:https://leetcode.com/problems/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 sq