c语言sscanf函数的用法是什么
277
2022-08-27
[leetcode] 202. Happy Number
Description
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:
Input: 19Output: trueExplanation: 1^2 + 9^2 = 828^2 + 2^2 = 686^2 + 8^2 = 1001^2 + 0^2 + 0^2 = 1
分析
题目的意思是:判断一个数是否是开心数。
这是leetcode上的easy题目。关键是怎么判断循环的存在,这里用了一个set存放已经出现过的数字,所以后面只需要判断数字在set是否出现过就可以判断是否有循环了,如果有循环,则数字一定会在set里面再出现,这样就找到了终止条件了。
代码
class Solution {public: bool isHappy(int n) { set
参考文献
[LeetCode] Happy Number 快乐数
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~