Skip to content

Commit b3f6648

Browse files
authored
Happy Number (codedecks-in#51)
* Happy Number * Power of Three * Power of Three * Solution without HashSet * Update README.md * Updated readme * Update README.md
1 parent 72228e1 commit b3f6648

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

Java/Happy-Number.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class HappyNumber {
2+
public boolean isHappy(int n) {
3+
int num1, num2;
4+
5+
num1 = num2 = n;
6+
do
7+
{
8+
num1 = SquareSum(num1);
9+
10+
11+
num2 = SquareSum(SquareSum(num2));
12+
13+
}
14+
while (num1 != num2);
15+
16+
return (num1 == 1);
17+
}
18+
19+
20+
static int SquareSum(int n)
21+
{
22+
int square = 0;
23+
while (n!= 0)
24+
{
25+
square += (n % 10) * (n % 10);
26+
n /= 10;
27+
}
28+
return square;
29+
}
30+
31+
}

Java/Power-of-Three.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean isPowerOfThree(int n) {
3+
4+
if(n < 1)
5+
{
6+
return false;
7+
}
8+
9+
while(n % 3 == 0)
10+
{
11+
n = n/3;
12+
}
13+
return n==1;
14+
}
15+
}

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
231231
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
232232
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [C++](./C++/Excel-Sheet-Column-Title.cpp) | _O(n)_ | _O(n)_ | Easy | String | |
233233
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java) <br> [C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
234-
234+
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
235+
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
235236
<br/>
236237
<div align="right">
237238
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -363,10 +364,12 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
363364
| [Girish Thatte](https://github.com/girishgr8/) <br> <img src="https://github.com/girishgr8.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/girish13/) <br> [Hackerrank](https://www.hackerrank.com/procoder_13) <br> [Codechef](https://www.codechef.com/procoder_13) |
364365
| [Kevin Chittilapilly](https://github.com/KevinChittilapilly) <br> <img src="https://github.com/KevinChittilapilly.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/being_kevin/) <br> [Hackerrank](https://www.hackerrank.com/ckevinvarghese11) <br> [Kaggle](https://www.kaggle.com/kevinchittilapilly) |
365366
| [Nour Grati](https://github.com/Nour-Grati) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | Tunisia | Python | [Leetcode](https://leetcode.com/nourgrati/) <br> [Hackerrank](https://www.hackerrank.com/noor_grati) <br> [Twitter](https://twitter.com/GratiNour1)
366-
|[Avinash Trivedi](https://github.com/trivediavinash) <br> <img src="https://github.com/trivediavinash.png" width="100" height="100"> |India | C++ | [Leetcode](https://leetcode.com/avi_002/) |
367-
|[Ishika Goel](https://github.com/ishikagoel5628) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishikagoel5628/) |
368-
|[Prashansa Tanwar](https://github.com/prashansatanwar) <br> <img src="https://github.com/prashansatanwar.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/prashansaaa/) |
369-
|[Ishu Raj](https://github.com/ir2010) <br> <img src="https://github.com/ir2010.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishuraj2010/) |
367+
| [Avinash Trivedi](https://github.com/trivediavinash) <br> <img src="https://github.com/trivediavinash.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/avi_002/) |
368+
| [Ishika Goel](https://github.com/ishikagoel5628) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishikagoel5628/) |
369+
| [Fenil Dobariya](https://github.com/ifenil) <br> <img src="https://rb.gy/iascx3" width="100" height="100"> | India | Java | [Github](https://github.com/ifenil) |
370+
| [Prashansa Tanwar](https://github.com/prashansatanwar) <br> <img src="https://github.com/prashansatanwar.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/prashansaaa/) |
371+
| [Ishu Raj](https://github.com/ir2010) <br> <img src="https://github.com/ir2010.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishuraj2010/) |
372+
370373

371374
<br/>
372375
<div align="right">

0 commit comments

Comments
 (0)