We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 699443e + 5a69916 commit fa24178Copy full SHA for fa24178
problems/0202.快乐数.md
@@ -215,6 +215,23 @@ var isHappy = function(n) {
215
}
216
return n === 1;
217
};
218
+
219
+// 方法四:使用Set(),求和用reduce
220
+var isHappy = function(n) {
221
+ let set = new Set();
222
+ let totalCount;
223
+ while(totalCount !== 1) {
224
+ let arr = (''+(totalCount || n)).split('');
225
+ totalCount = arr.reduce((total, num) => {
226
+ return total + num * num
227
+ }, 0)
228
+ if (set.has(totalCount)) {
229
+ return false;
230
+ }
231
+ set.add(totalCount);
232
233
+ return true;
234
+};
235
```
236
237
Swift:
0 commit comments