forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
202.c
100 lines (87 loc) · 2.21 KB
/
202.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct HashNode {
int key; /* num */
struct HashNode *next;
};
static inline int hash(int key, int size) {
int index = key % size;
return (index > 0) ? (index) : (-index);
}
bool isHappy_1(int n) {
if (n == 1) return true;
int i;
bool isHappy = false;
int size = 20;
struct HashNode **hashtable
= (struct HashNode **)calloc(size, sizeof(struct HashNode*));
while (1) {
/* check if n has been calculated */
int index = hash(n, size);
struct HashNode **p = hashtable + index;
while (*p){
if ((*p)->key == n) { /* found, endless loop, n is not happy */
isHappy = false;
goto OUT;
}
p = &((*p)->next);
}
/* put n into hash table */
struct HashNode *new_node
= (struct HashNode*)malloc(sizeof(struct HashNode));
new_node->key = n;
new_node->next = NULL;
*p = new_node;
int t = 0;
while (n) {
t += (n % 10) * (n % 10);
n /= 10;
}
if (t == 1) {
isHappy = true;
goto OUT;
}
else n = t;
}
OUT:
/* free memory */
for (i = 0; i < size; i++) {
struct HashNode *t = hashtable[i];
struct HashNode *x = NULL;
while (t) {
x = t->next;
free(t);
t = x;
}
}
free(hashtable);
return isHappy;
}
/**
* If a number is NOT happy, there is always a 4 in the cycle.
* https://en.wikipedia.org/wiki/Happy_number#Sequence_behavior
*/
bool isHappy(int n) {
if (n <= 0) return false;
int magic = 4;
while (1) {
if (n == 1) return true;
if (n == magic) return false;
int t = 0;
while (n) {
t += (n % 10) * (n % 10);
n /= 10;
}
n = t;
}
}
int main() {
/* 19 is happy */
printf("%d\n", isHappy(19));
/* 1 is happy */
printf("%d\n", isHappy(1));
/* 0 is NOT happy */
printf("%d\n", isHappy(0));
return 0;
}