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

题目也是比较简单,AC代码如下:

bool isHappy(int n) {
        if(n<=0)
            return false;
        else if(n==1)
            return true;
        else
        {
            int squSum=n;
            int LUT[10];
            for(int i=0;i<10;i++)
                LUT[i] = i*i;
            vector<int> sum;
            while(squSum!=1)
            {
                sum.push_back(squSum);
                int tmp=squSum;
                squSum=0;
                while(tmp>=10)
                {
                    squSum=squSum+LUT[tmp%10];
                    tmp=tmp/10;
                }
                squSum=squSum+LUT[tmp];
                if(squSum == 1)
                    return true;
                vector<int>::iterator result = find( sum.begin( ), sum.end( ), squSum);
                if(result != sum.end())
                    break;
            }
            return false;
        }
    }

注意下,用到了查找表,能省点时间就省点吧。

时间: 2024-12-11 02:43:51

LeetCode【202】Happy Number的相关文章

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number 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 spa

LeetCode【9】Palindrome Number

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

【Leetcode长征系列】Single Number II

原题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<

【leetcode78】Single Number II

题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra m

leetcode第202题-Happy Number

说实话,如果不看别人的解答的话,这道题我也是没有思路,不知道该循环几次,也不知道循环的终止条件,后来才知道,[2-6]这个范围内的数字都不是happy number 所以就有了终止条件,n>6就是终止条件,当n跳进这个范围内的时候就终止循环,最后就能判断是否是happy number了 #include<stdio.h> #include<stdlib.h> bool isHappy(int n) { while(n>6) { int sum=0; while(n) {

【Leetcode】【Hard】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 ambiguous. You

【leetcode79】Single Number III

题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: G

【数组】Missing Number

题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it