添加 0202.快乐数 PHP版本

This commit is contained in:
nolanzzz
2021-09-02 23:49:37 -04:00
parent 4a78d79db0
commit 485f6a806a

View File

@ -223,6 +223,37 @@ func isHappy(_ n: Int) -> Bool {
}
```
PHP:
```php
class Solution {
/**
* @param Integer $n
* @return Boolean
*/
function isHappy($n) {
// use a set to record sum
// whenever there is a duplicated, stop
// == 1 return true, else false
$table = [];
while ($n != 1 && !isset($table[$n])) {
$table[$n] = 1;
$n = self::getNextN($n);
}
return $n == 1;
}
function getNextN(int $n) {
$res = 0;
while ($n > 0) {
$temp = $n % 10;
$res += $temp * $temp;
$n = floor($n / 10);
}
return $res;
}
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)