-
Notifications
You must be signed in to change notification settings - Fork 98
Added Happy Number #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d3bc062
74d4041
7d77208
d721505
828779a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
Happy Number | ||
https://leetcode.com/problems/happy-number/ | ||
|
||
Write an algorithm to determine if a number n 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. | ||
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. | ||
|
||
Return true if n is a happy number, and false if not. | ||
|
||
Example 1: | ||
Input: n = 19 | ||
Output: true | ||
Explanation: | ||
12 + 92 = 82 | ||
82 + 22 = 68 | ||
62 + 82 = 100 | ||
12 + 02 + 02 = 1 | ||
|
||
Example 2: | ||
Input: n = 2 | ||
Output: false | ||
|
||
|
||
*/ | ||
|
||
/** | ||
* @param {number} n | ||
* @return {boolean} | ||
*/ | ||
var isHappy = function(n) { | ||
return checkHappyNumber(n); | ||
}; | ||
|
||
function checkHappyNumber(n){ | ||
strNumber = n.toString(); | ||
splitNumber = strNumber.split(""); | ||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another solution that doesn't require translating the integer to a String would be to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, i don't get it. could you give an example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if(splitNumber.length <= 1){ | ||
return (n <= 1)? true:false; | ||
} | ||
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0); | ||
return checkHappyNumber(digit) | ||
} | ||
|
||
module.exports.isHappy = isHappy; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const assert = require('assert'); | ||
const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy; | ||
|
||
var test = function () { | ||
assert.equal(isHappy(19),true); | ||
assert.equal(isHappy(2),false); | ||
} | ||
|
||
module.exports.test = test; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update this using a
^
? The digits together are a bit confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure