Leetcode Reflection 3.16-3.22
Hi this is Di again. I realize it's too clumsy to post for every leetcode problem I solved. Therefore I decided to accumalate one weeks' reflection together and post them once. 202. Happy Number If...

Source: DEV Community
Hi this is Di again. I realize it's too clumsy to post for every leetcode problem I solved. Therefore I decided to accumalate one weeks' reflection together and post them once. 202. Happy Number If we add on all the square of a number's every digit, it end up with result 1, we call this number is a happy number. It's easy to end the loop by detecting the emerge of 1. But how can we know when to end the endless loop without knowing whether 1 will appear? Here is the maths prove behind it: For one digit numbers, the max is 9, its square sum is 81, far bigger than 9. For two digit numbers, the max is 99, its square sum is 81+81=162, slightly bigger than 99. For three digit numbers, the max is 999, its square sum is 81x3=243, far less than 999 For four digit numbers, the max is 9999, its square sum is 81x4=324, far less than 9999 No matter how large the number is, after first calculation, it will collapse into range 1-999. Then turn into 1-243 in next iteration, then converged into this ra