mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
添加 0202.快乐数 PHP版本
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user