Skip to content

Commit 727c2f2

Browse files
committed
leetcode
1 parent e7a160c commit 727c2f2

File tree

4 files changed

+397
-0
lines changed

4 files changed

+397
-0
lines changed

HappyNumber/happy_number.dart

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
3+
-* Happy Number *-
4+
5+
Write an algorithm to determine if a number n is happy.
6+
7+
A happy number is a number defined by the following process:
8+
9+
Starting with any positive integer, replace the number by the sum of the squares of its digits.
10+
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
11+
Those numbers for which this process ends in 1 are happy.
12+
Return true if n is a happy number, and false if not.
13+
14+
15+
16+
Example 1:
17+
18+
Input: n = 19
19+
Output: true
20+
Explanation:
21+
12 + 92 = 82
22+
82 + 22 = 68
23+
62 + 82 = 100
24+
12 + 02 + 02 = 1
25+
Example 2:
26+
27+
Input: n = 2
28+
Output: false
29+
30+
31+
Constraints:
32+
33+
1 <= n <= 231 - 1
34+
35+
*/
36+
37+
import 'dart:collection';
38+
import 'dart:math';
39+
40+
class A {
41+
/*
42+
43+
Complexity Analysis:
44+
45+
Time Complexity: O(n*log(n)).
46+
Auxiliary Space: O(1).
47+
*/
48+
// Runtime: 559 ms, faster than 36.59% of Dart online submissions for Happy Number.
49+
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
50+
51+
bool isHappy(int n) {
52+
int slow, fast;
53+
// initialize slow and fast by n
54+
slow = fast = n;
55+
do {
56+
// move slow number
57+
// by one iteration
58+
slow = numSquareSum(slow);
59+
// move fast number
60+
// by two iteration
61+
fast = numSquareSum(numSquareSum(fast));
62+
} while (slow != fast);
63+
return (slow == 1);
64+
}
65+
66+
// Utility method to return sum of square of
67+
// digit of n
68+
int numSquareSum(int n) {
69+
int sumSquare = 0;
70+
while (n != 0) {
71+
sumSquare += (n % 10) * (n % 10);
72+
n ~/= 10;
73+
}
74+
return sumSquare;
75+
}
76+
}
77+
78+
class B {
79+
/*
80+
Time Complexity: O(n*log(n)).
81+
Auxiliary Space: O(n) since using extra set for storage
82+
*/
83+
// Runtime: 536 ms, faster than 53.66% of Dart online submissions for Happy Number.
84+
// Memory Usage: 143.7 MB, less than 29.27% of Dart online submissions for Happy Number.
85+
bool isHappy(int n) {
86+
HashSet hs = HashSet();
87+
while (true) {
88+
n = numSquareSum(n);
89+
if (n == 1) return true;
90+
if (hs.contains(n)) return false;
91+
hs.add(n);
92+
}
93+
}
94+
95+
int numSquareSum(int n) {
96+
int sumSquare = 0;
97+
while (n != 0) {
98+
sumSquare += (n % 10) * (n % 10);
99+
n ~/= 10;
100+
}
101+
return sumSquare;
102+
}
103+
}
104+
105+
class C {
106+
// Runtime: 484 ms, faster than 78.05% of Dart online submissions for Happy Number.
107+
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
108+
109+
bool isHappy(int n) {
110+
if (n == 1 || n == 7) return true;
111+
int sum = n, x = n;
112+
113+
// this loop executes till the sum of square of
114+
// digits obtained is not a single digit number
115+
while (sum > 9) {
116+
sum = 0;
117+
118+
// this loop finds the sum of square of digits
119+
while (x > 0) {
120+
int d = x % 10;
121+
sum += d * d;
122+
x ~/= 10;
123+
}
124+
if (sum == 1) return true;
125+
x = sum;
126+
}
127+
if (sum == 7) return true;
128+
return false;
129+
}
130+
}
131+
132+
class D {
133+
// Runtime: 261 ms, faster than 100.00% of Dart online submissions for Happy Number.
134+
// Memory Usage: 139.3 MB, less than 100.00% of Dart online submissions for Happy Number.
135+
/// > It takes a number, adds the squares of its digits, and returns the result
136+
///
137+
/// Args:
138+
/// n (int): The number to be checked.
139+
///
140+
/// Returns:
141+
/// The sum of the squares of the digits of the number.
142+
int solve(int n) {
143+
int sum = 0;
144+
for (int i = 0; n != 0; i++) {
145+
int r = n % 10;
146+
sum = sum + r * r;
147+
n = n ~/ 10;
148+
}
149+
150+
return sum;
151+
}
152+
153+
bool isHappy(int n) {
154+
int i = 10;
155+
156+
/// A loop that checks if the number is happy.
157+
while (i > 0) {
158+
int happy = solve(n);
159+
if (happy == 1) {
160+
return true;
161+
}
162+
n = happy;
163+
i--;
164+
}
165+
return false;
166+
}
167+
}
168+
169+
class E {
170+
// Runtime: 309 ms, faster than 100.00% of Dart online submissions for Happy Number.
171+
// Memory Usage: 158 MB, less than 7.32% of Dart online submissions for Happy Number.
172+
int sums(int n) {
173+
int ans = 0;
174+
while (n != 0) {
175+
//ans += pow(n % 10, 2);
176+
ans += pow(n % 10, 2).floor();
177+
n = n ~/ 10;
178+
}
179+
return ans;
180+
}
181+
182+
bool isHappy(int n) {
183+
int s = sums(n);
184+
int f = sums(sums(n));
185+
if (f == 1) return true; //for case n==1
186+
while (s != f) {
187+
if (f == 1) return true;
188+
s = sums(s);
189+
f = sums(sums(f));
190+
}
191+
return false;
192+
}
193+
}

HappyNumber/happy_number.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
/*
4+
Runtime: 0 ms, faster than 100.00% of Go online submissions for Happy Number.
5+
Memory Usage: 2 MB, less than 93.92% of Go online submissions for Happy Number.
6+
*/
7+
8+
func isHappy(n int) bool {
9+
i := 10
10+
for i > 0 {
11+
happy := sums(n)
12+
if happy == 1 {
13+
return true
14+
}
15+
n = happy
16+
i--
17+
}
18+
return false
19+
}
20+
func sums(n int) int {
21+
var sum int = 0
22+
for i := 0; n != 0; i++ {
23+
r := n % 10
24+
sum = sum + r*r
25+
n = n / 10
26+
}
27+
return sum
28+
}

HappyNumber/happy_number.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# 🔥 Happy Number 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
### Complexity Analysis
6+
7+
- Time Complexity: O(n*log(n)).
8+
- Auxiliary Space: O(1).
9+
10+
```dart
11+
class Solution {
12+
// Runtime: 559 ms, faster than 36.59% of Dart online submissions for Happy Number.
13+
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
14+
15+
bool isHappy(int n) {
16+
int slow, fast;
17+
// initialize slow and fast by n
18+
slow = fast = n;
19+
do {
20+
// move slow number
21+
// by one iteration
22+
slow = numSquareSum(slow);
23+
// move fast number
24+
// by two iteration
25+
fast = numSquareSum(numSquareSum(fast));
26+
} while (slow != fast);
27+
return (slow == 1);
28+
}
29+
// Utility method to return sum of square of
30+
// digit of n
31+
int numSquareSum(int n) {
32+
int sumSquare = 0;
33+
while (n != 0) {
34+
sumSquare += (n % 10) * (n % 10);
35+
n ~/= 10;
36+
}
37+
return sumSquare;
38+
}
39+
```
40+
41+
## Solution - 2
42+
43+
### Complexity-Analysis
44+
45+
- Time Complexity: O(n*log(n)).
46+
- Auxiliary Space: O(n) since using extra set for storage
47+
48+
```dart
49+
class Solution {
50+
// Runtime: 536 ms, faster than 53.66% of Dart online submissions for Happy Number.
51+
// Memory Usage: 143.7 MB, less than 29.27% of Dart online submissions for Happy Number.
52+
53+
bool isHappy(int n) {
54+
HashSet hs = HashSet();
55+
while (true) {
56+
n = numSquareSum(n);
57+
if (n == 1) return true;
58+
if (hs.contains(n)) return false;
59+
hs.add(n);
60+
}
61+
}
62+
63+
int numSquareSum(int n) {
64+
int sumSquare = 0;
65+
while (n != 0) {
66+
sumSquare += (n % 10) * (n % 10);
67+
n ~/= 10;
68+
}
69+
return sumSquare;
70+
}
71+
}
72+
```
73+
74+
## Solution - 3
75+
76+
```dart
77+
class Solution {
78+
// Runtime: 484 ms, faster than 78.05% of Dart online submissions for Happy Number.
79+
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
80+
81+
bool isHappy(int n) {
82+
if (n == 1 || n == 7) return true;
83+
int sum = n, x = n;
84+
85+
// this loop executes till the sum of square of
86+
// digits obtained is not a single digit number
87+
while (sum > 9) {
88+
sum = 0;
89+
90+
// this loop finds the sum of square of digits
91+
while (x > 0) {
92+
int d = x % 10;
93+
sum += d * d;
94+
x ~/= 10;
95+
}
96+
if (sum == 1) return true;
97+
x = sum;
98+
}
99+
if (sum == 7) return true;
100+
return false;
101+
}
102+
}
103+
```
104+
105+
## Solution - 4
106+
107+
```dart
108+
class Solution {
109+
// Runtime: 261 ms, faster than 100.00% of Dart online submissions for Happy Number.
110+
// Memory Usage: 139.3 MB, less than 100.00% of Dart online submissions for Happy Number.
111+
/// > It takes a number, adds the squares of its digits, and returns the result
112+
///
113+
/// Args:
114+
/// n (int): The number to be checked.
115+
///
116+
/// Returns:
117+
/// The sum of the squares of the digits of the number.
118+
int solve(int n) {
119+
int sum = 0;
120+
for (int i = 0; n != 0; i++) {
121+
int r = n % 10;
122+
sum = sum + r * r;
123+
n = n ~/ 10;
124+
}
125+
126+
return sum;
127+
}
128+
129+
bool isHappy(int n) {
130+
int i = 10;
131+
132+
/// A loop that checks if the number is happy.
133+
while (i > 0) {
134+
int happy = solve(n);
135+
if (happy == 1) {
136+
return true;
137+
}
138+
n = happy;
139+
i--;
140+
}
141+
return false;
142+
}
143+
}
144+
```
145+
146+
## SOlution 5
147+
148+
```dart
149+
class Solution {
150+
// Runtime: 309 ms, faster than 100.00% of Dart online submissions for Happy Number.
151+
// Memory Usage: 158 MB, less than 7.32% of Dart online submissions for Happy Number.
152+
int sums(int n) {
153+
int ans = 0;
154+
while (n != 0) {
155+
//ans += pow(n % 10, 2);
156+
ans += pow(n % 10, 2).floor();
157+
n = n ~/ 10;
158+
}
159+
return ans;
160+
}
161+
162+
bool isHappy(int n) {
163+
int s = sums(n);
164+
int f = sums(sums(n));
165+
if (f == 1) return true; //for case n==1
166+
while (s != f) {
167+
if (f == 1) return true;
168+
s = sums(s);
169+
f = sums(sums(f));
170+
}
171+
return false;
172+
}
173+
}
174+
175+
```

0 commit comments

Comments
 (0)