How to Solve '202 Happy Number' on LeetCode? - Javascript

preview_player
Показать описание
Do you need more help with coding?
════════════════════════════

Problem: #202 Happy Number
Language: #Javascript
Difficulty: Easy

Strategy: Use hash table.
Time Complexity: O(n^2)
Space Complexity: O(n)

Pseudo Code:
1. Create map object.
2. While loop through "n" being not equal to 1.
a. Create a current variable equal to "n".
b. Create sum variable.
c. While loop for current being not equal to 0.
i. Add to sum the squared of (current mod 10).
ii. Divide current by 10.
d. Condition if sum is in the map.
i. Return false.
e. Add sum to map with a value of true.
f. Set n = sum.
3. Return true.

Let's Connect 💯:
════════════════════════════
Рекомендации по теме
Комментарии
Автор

Thank you for this clear, straight forward solution!! Do you use any specific resources to guide your algo work, or are you just doing problems as you find them on LeetCode?

hproctor