博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode Happy Number
阅读量:4585 次
发布时间:2019-06-09

本文共 880 字,大约阅读时间需要 2 分钟。

题目连接

  

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: 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$

class Solution {public:	bool isHappy(int n) {		if (1 == n) return true;		set
A; while (true) { int v = 0; do { int i = n % 10; v += i * i; } while (n /= 10); if (1 == v) return true; if (A.find(v) != A.end()) return false; n = v; A.insert(n); } }};

转载于:https://www.cnblogs.com/GadyPu/p/5028183.html

你可能感兴趣的文章
jetty加载spring-context容器源码分析
查看>>
方向ajax(http long request实现实时通信)
查看>>
[LeetCode&Python] Problem 746. Min Cost Climbing Stairs
查看>>
如何在DOS窗口复制和粘贴命令
查看>>
总结一下技能
查看>>
css 一张图片做导航
查看>>
个人项目耗时对比记录表
查看>>
评判是人工智能性能的准则
查看>>
XACT_ABORT为默认为off事务发生约束性错误会继续运行,为on发生约束性错误时会rollback...
查看>>
MySQL备份和还原
查看>>
设计模式之建造者模式(简单)
查看>>
BootBox使用
查看>>
B. Pyramid of Glasses
查看>>
数据库设计常考题目简要分析
查看>>
《C++ Primer Plus(第6版)》14章 C++代码复用 - 代码清单14.3
查看>>
C#关于软件界面无响应、BUG报警、程序异常退出等情况的监控和报警
查看>>
Linux中获取root权限及关机重启语法
查看>>
个人阅读作业2
查看>>
内置函数
查看>>
js基础之DOM中元素对象的属性方法
查看>>