-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3bc062
Added Happy Number
anonimak 74d4041
update explanation square using ^
anonimak 7d77208
Added Shuffle_String problem solution
anonimak d721505
Added Shuffle_String problem test
anonimak 828779a
Added Shuffle_String problem to the README
anonimak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
1^2 + 9^2 = 82 | ||
8^2 + 2^2 = 68 | ||
6^2 + 8^2 = 100 | ||
1^2 + 0^2 + 0^2 = 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(""); | ||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. | ||
|
||
Return the shuffled string. | ||
|
||
|
||
|
||
Example 1: | ||
|
||
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] | ||
Output: "leetcode" | ||
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. | ||
|
||
Example 2: | ||
|
||
Input: s = "abc", indices = [0,1,2] | ||
Output: "abc" | ||
Explanation: After shuffling, each character remains in its position. | ||
|
||
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. I guess this assumes the input indices is a valid input e.g. it cannot be [-1, 1023234234] |
||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @param {number[]} indices | ||
* @return {string} | ||
*/ | ||
var restoreString = function(s, indices) { | ||
let arrshuffle = []; | ||
indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)) | ||
return arrshuffle.join('') | ||
}; | ||
|
||
module.exports.restoreString = restoreString; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const assert = require('assert'); | ||
const restoreString = require('../../LeetcodeProblems/Algorithms/Shuffle_String').restoreString; | ||
|
||
var test = function () { | ||
assert.equal(restoreString('codeleet',[4,5,6,7,0,2,1,3]),'leetcode'); | ||
assert.equal(restoreString('abc',[0,1,2]),'abc'); | ||
assert.equal(restoreString('hacktoberfest',[9,10,11,12,4,5,6,7,8,0,1,2,3]),'festtoberhack'); | ||
} | ||
|
||
module.exports.test = test; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
another solution that doesn't require translating the integer to a String would be to
get the number, apply mod 10 and divide it by 10, get the digit and repeat the process.
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.
sorry, i don't get it. could you give an example?
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.