File tree Expand file tree Collapse file tree 3 files changed +164
-0
lines changed
solution/0200-0299/0202.Happy Number Expand file tree Collapse file tree 3 files changed +164
-0
lines changed Original file line number Diff line number Diff line change @@ -154,6 +154,29 @@ func getSum(n int) (sum int) {
154
154
155
155
### ** TypeScript**
156
156
157
+ ``` ts
158
+ function isHappy(n : number ): boolean {
159
+ const getNext = (n : number ) => {
160
+ let res = 0 ;
161
+ while (n !== 0 ) {
162
+ res += (n % 10 ) ** 2 ;
163
+ n = Math .floor (n / 10 );
164
+ }
165
+ return res ;
166
+ };
167
+ const set = new Set ();
168
+ while (n !== 1 ) {
169
+ const next = getNext (n );
170
+ if (set .has (next )) {
171
+ return false ;
172
+ }
173
+ set .add (next );
174
+ n = next ;
175
+ }
176
+ return true ;
177
+ }
178
+ ```
179
+
157
180
``` ts
158
181
function isHappy(n : number ): boolean {
159
182
const getNext = (n : number ) => {
@@ -177,6 +200,33 @@ function isHappy(n: number): boolean {
177
200
178
201
### ** Rust**
179
202
203
+ ``` rust
204
+ use std :: collections :: HashSet ;
205
+ impl Solution {
206
+ fn get_next (mut n : i32 ) -> i32 {
207
+ let mut res = 0 ;
208
+ while n != 0 {
209
+ res += (n % 10 ). pow (2 );
210
+ n /= 10 ;
211
+ }
212
+ res
213
+ }
214
+
215
+ pub fn is_happy (mut n : i32 ) -> bool {
216
+ let mut set = HashSet :: new ();
217
+ while n != 1 {
218
+ let next = Self :: get_next (n );
219
+ if set . contains (& next ) {
220
+ return false ;
221
+ }
222
+ set . insert (next );
223
+ n = next ;
224
+ }
225
+ true
226
+ }
227
+ }
228
+ ```
229
+
180
230
``` rust
181
231
impl Solution {
182
232
pub fn is_happy (n : i32 ) -> bool {
@@ -199,6 +249,29 @@ impl Solution {
199
249
}
200
250
```
201
251
252
+ ### ** C**
253
+
254
+ ``` c
255
+ int getNext (int n) {
256
+ int res = 0;
257
+ while (n) {
258
+ res += (n % 10) * (n % 10);
259
+ n /= 10;
260
+ }
261
+ return res;
262
+ }
263
+
264
+ bool isHappy(int n) {
265
+ int slow = n;
266
+ int fast = getNext(n);
267
+ while (slow != fast) {
268
+ slow = getNext(slow);
269
+ fast = getNext(getNext(fast));
270
+ }
271
+ return fast == 1;
272
+ }
273
+ ```
274
+
202
275
### **...**
203
276
204
277
```
Original file line number Diff line number Diff line change @@ -138,6 +138,29 @@ func getSum(n int) (sum int) {
138
138
139
139
### ** TypeScript**
140
140
141
+ ``` ts
142
+ function isHappy(n : number ): boolean {
143
+ const getNext = (n : number ) => {
144
+ let res = 0 ;
145
+ while (n !== 0 ) {
146
+ res += (n % 10 ) ** 2 ;
147
+ n = Math .floor (n / 10 );
148
+ }
149
+ return res ;
150
+ };
151
+ const set = new Set ();
152
+ while (n !== 1 ) {
153
+ const next = getNext (n );
154
+ if (set .has (next )) {
155
+ return false ;
156
+ }
157
+ set .add (next );
158
+ n = next ;
159
+ }
160
+ return true ;
161
+ }
162
+ ```
163
+
141
164
``` ts
142
165
function isHappy(n : number ): boolean {
143
166
const getNext = (n : number ) => {
@@ -161,6 +184,33 @@ function isHappy(n: number): boolean {
161
184
162
185
### ** Rust**
163
186
187
+ ``` rust
188
+ use std :: collections :: HashSet ;
189
+ impl Solution {
190
+ fn get_next (mut n : i32 ) -> i32 {
191
+ let mut res = 0 ;
192
+ while n != 0 {
193
+ res += (n % 10 ). pow (2 );
194
+ n /= 10 ;
195
+ }
196
+ res
197
+ }
198
+
199
+ pub fn is_happy (mut n : i32 ) -> bool {
200
+ let mut set = HashSet :: new ();
201
+ while n != 1 {
202
+ let next = Self :: get_next (n );
203
+ if set . contains (& next ) {
204
+ return false ;
205
+ }
206
+ set . insert (next );
207
+ n = next ;
208
+ }
209
+ true
210
+ }
211
+ }
212
+ ```
213
+
164
214
``` rust
165
215
impl Solution {
166
216
pub fn is_happy (n : i32 ) -> bool {
@@ -183,6 +233,29 @@ impl Solution {
183
233
}
184
234
```
185
235
236
+ ### ** C**
237
+
238
+ ``` c
239
+ int getNext (int n) {
240
+ int res = 0;
241
+ while (n) {
242
+ res += (n % 10) * (n % 10);
243
+ n /= 10;
244
+ }
245
+ return res;
246
+ }
247
+
248
+ bool isHappy(int n) {
249
+ int slow = n;
250
+ int fast = getNext(n);
251
+ while (slow != fast) {
252
+ slow = getNext(slow);
253
+ fast = getNext(getNext(fast));
254
+ }
255
+ return fast == 1;
256
+ }
257
+ ```
258
+
186
259
### **...**
187
260
188
261
```
Original file line number Diff line number Diff line change
1
+ int getNext (int n ) {
2
+ int res = 0 ;
3
+ while (n ) {
4
+ res += (n % 10 ) * (n % 10 );
5
+ n /= 10 ;
6
+ }
7
+ return res ;
8
+ }
9
+
10
+ bool isHappy (int n ) {
11
+ int slow = n ;
12
+ int fast = getNext (n );
13
+ while (slow != fast ) {
14
+ slow = getNext (slow );
15
+ fast = getNext (getNext (fast ));
16
+ }
17
+ return fast == 1 ;
18
+ }
You can’t perform that action at this time.
0 commit comments