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

分析

我原以为这道题难点只是在于把那些那些数字包含的数都找出来,也就是说给出19找出1和9,后来发现还有一个难点……

int getSum(int num) {
    vector<int> digits;
    while (num > 9) {
        digits.push_back(num % 10);
        num = num / 10;
    }
    digits.push_back(num);
    int sum = 0;
    for (int i = 0; i < digits.size(); i++) {
        sum += digits[i] * digits[i];
    }
    return sum;
}

这里用vector来保存了每个数字,然后将其求平方和。下面是一个递归,多嵌套了一层,难度其实也不大,然而提交之后发现还是错了。

bool isHappy(int n) {
    if (n == 1) return true;
    else return isHappy(getSum(n));
    return false;
}

反馈的结果是说2不能通过,试了试:

2,4,16,27,58……最后又是4,16成了死循环了

喔喔,题意中确实提到了:

或者它在一个不包含1的周期内无限循环

想了好久没有想到什么办法能够找出死循环的判断方法,于是只能去找规律了。发现对于:

2,4,8,3,6,9,5

都会造成死循环,于是干脆加上一个判断条件,n等于这些数字时直接返回false。果然通过了……

代码

class Solution {
public:
    int getSum(int num) {
        vector<int> digits;
        while (num > 9) {
            digits.push_back(num % 10);
            num = num / 10;
        }
        digits.push_back(num);
        int sum = 0;
        for (int i = 0; i < digits.size(); i++) {
            sum += digits[i] * digits[i];
        }
        return sum;
    }

    bool isHappy(int n) {
        if (n == 1) return true;
        else if (n != 7 && (2 <= n && n <= 9)) return false;
        else return isHappy(getSum(n));
        return false;
    }
};

进阶

不过我对这种解法还不太满意,因为虽然可以找规律,但毕竟要花蛮多的时间。去学习学习别人的解法吧……

bool isHappy(int n) {
    unordered_set<int> visited;
    while (n != 1) {
        if (visited.find(n) != visited.end())
            return false;
        visited.insert(n);
        int sum = 0;
        while (n) {
            sum += ((n % 10)*(n % 10));
            n /= 10;
        }
        n = sum;
    }
    return true;
}

这个解法就是完美解决了死循环的问题,用unordered_set的find函数可以很快判断一个数字是否已经访问过了,如果是的话肯定直接false了。

后面的while循环部分则和我的大同小异了 。

时间: 2024-11-03 21:08:15

LeetCode 202 Happy Number(开心数)(vector、unordered_set)的相关文章

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

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

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 弗洛伊德判环

弗洛伊德判环! 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[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 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