LeetCode - Problems - Algorithms - 202. Happy Number
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
My Solution (C#)
static public bool IsHappy(int n)
{
List<int> tracks = new List<int>();
while (!tracks.Contains(n))
{
tracks.Add(n);
int remainder = 0;
int quotient = n;
int sum = 0;
do {
sum += remainder * remainder;
remainder = n % 10;
quotient = n / 10;
n = quotient;
} while (quotient >= 10);
n = sum + quotient * quotient + remainder * remainder;
}
if (n == 1) return true;
return false;
}
My Explanation
// 19> 82 >68 >100 > 1: it will repeat 1. so, happy number
// 2> 4 >8 >16 >37 >58 >89 >145 >42 >20 > 4: it will repeat and never make result 1. so, it is unhappy
//the list to record the result of adding squares of n in
//if the list contains n, it means it terminates this loop. so while the statement to repeat while the track does not contain n
// before starting the set of while statements we need to add n into the list, tracks to remember it for later to check if it passed or not
//an integer variable remainder by dividing 10.
//quotient that is dropped the remainder. but this will also need for checking if the quotient is a two-digit number later.
//also an integer variable sum to add each digit square.
// those three variables need to be initialized every time of result.
// square the remainder obtained from the previous loop and add it.
//define remainder n's last digit
//define the digit number except for the last digit
//define n as a quotient that is used to check whether the loop needs to be repeated until n has only one digit
//this do-while statement is for repeating to add the last digit square to the total sum. we are using a do-while statement because this should be done before checking the condition of the loop. otherwise, we will miss the last step of addition.
//the condition means there is only one digit. without this condition, this will be an infinite loop.
// we had been adding the numbers from back to front. so now, the quotient is the first digit of the n
}
//if n is contained in the list, (of course when n is 1 because when n is 1 the next n will be 1) we can escape the first while loop when the list contains n. when the number reaches 1, n is a happy number. returns true!
//otherwise false because the track is repeated infinitely between n to n.
}
Time complexity: O(1): when it repeats digit by digit the number of n's digits is reduced.
Space complexity O(1): this solution is been doing all of the calculation in a limited space
This script is written for the algorithm study group meeting.
Not confirmed professional just me who is still in junior level.
'Problem solving > Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 733. Flood Fill (C#)๏ปฟ (2) | 2025.07.09 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค]์ฝ์์ ํฉ (C#) (1) | 2025.07.08 |
[LeetCode] 338. Counting Bits (C#) (1) | 2024.01.13 |
[LeetCode] 125. Same Tree (C#) (0) | 2024.01.12 |
[LeetCode] 100. Valid Palindrome (C#) (0) | 2024.01.12 |