-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathE_HappyNumber.js
50 lines (41 loc) · 1.37 KB
/
E_HappyNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// xiaoli's solution - ac 459ms
/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
var digits = n.toString().split('');//split digits of n
//console.log(digits);
var number = 0;
var startTime = new Date().getTime(); //fetch starting time
while(new Date().getTime() - startTime < 1 && number != 1 ){
number = 0;
for(var i = 0; i < digits.length; i++){
number += digits[i]*digits[i];
}
digits = number.toString().split('');
//console.log(number);
}
if(number == 1) return true;
else return false;
};
var n = 19;
console.log(isHappy(n));
//ref: https://leetcode.com/discuss/34598/my-straightforward-solution-in-js
// Basically, I keep calculating the sum of squares until I come across one that I've already seen.
// The nice thing about this approach is that it memoizes all values so if this were used on multiple numbers,
// in the event that during the process a previously seen number is encountered, an answer can immediately be given.
// ac - 153ms
var isHappy = function(n) {
var seen = {};
while (n !== 1 && !seen[n]) {
seen[n] = true;
n = sumOfSquares(n);
}
return n === 1 ? true : false;
};
function sumOfSquares(numString) {
return numString.toString().split('').reduce(function(sum, num) {
return sum + Math.pow(num, 2);
}, 0);
}