728x90
Above is the link to the problem.
Problem
A happy number is a number defined by the following process
1. Starting with any positive integer, replace the number with the sum of the squares of its digits.
2. Repeat the process until the number equals 1 → Happy number : Return True,
or it loops endlessly in a cycle that does not include 1 → Unhappy number : Return False.
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
Explanation:
2^2 = 4
4^2 = 16
1^2 + 6^2 = 37
3^2 + 7^2 = 58
5^2 + 8^2 = 89
8^2 + 9^2 = 145
1^2 + 4^2 +5^2 = 42
4^2 + 2^2 = 20
2^2 + 0^2 = 4
→ endless loop
Code
This code gets the top 37.81% of time complexity and uses 13.7MB of memory.
728x90
반응형
'Code > LeetCode' 카테고리의 다른 글
[LeetCode] 605. Can Place Flowers (Easy/Python) (0) | 2023.03.20 |
---|---|
[LeetCode] 2187. Minimum Time to Complete Trips (Medium/Python) (0) | 2023.03.08 |
[LeetCode] 28. Find the Index of the First Occurrence in a String (Medium/Python) (0) | 2023.03.03 |
[LeetCode] 191. Number of 1 Bits (Easy/Python) (0) | 2023.03.02 |
[LeetCode] 190. Reverse Bits (Easy/Python) (0) | 2023.01.15 |