Skip to content

Commit da962db

Browse files
Improve
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent ce13413 commit da962db

File tree

13 files changed

+244
-33
lines changed

13 files changed

+244
-33
lines changed

202_happy_number/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test happy_number.c

202_happy_number/happy_number.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
static bool recursive(int n, bool *used, const int *sq)
7+
{
8+
if (n == 1) return true;
9+
if (n < 10000) {
10+
if (used[n]) return false;
11+
used[n] = true;
12+
}
13+
int sum = 0;
14+
while (n > 0) {
15+
sum += sq[n % 10];
16+
n /= 10;
17+
}
18+
return recursive(sum, used, sq);
19+
}
20+
21+
static bool isHappy(int n)
22+
{
23+
bool used[10000] = { false };
24+
const static int sq[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
25+
return recursive(n, used, sq);
26+
}
27+
28+
int main(int argc, char **argv)
29+
{
30+
if (argc != 2) {
31+
fprintf(stderr, "Usage: ./test n\n");
32+
exit(-1);
33+
}
34+
35+
printf("%s\n", isHappy(atoi(argv[1])) ? "true" : "false");
36+
return 0;
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test rm_elem.c
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
static struct ListNode *removeElement(struct ListNode *head, int val)
10+
{
11+
struct ListNode dummy;
12+
dummy.next = head;
13+
struct ListNode *prev = &dummy;
14+
struct ListNode *p = head;
15+
while (p != NULL) {
16+
if (p->val == val) {
17+
prev->next = p->next;
18+
} else {
19+
prev = p;
20+
}
21+
p = p->next;
22+
}
23+
return dummy.next;
24+
}
25+
26+
int main(int argc, char **argv)
27+
{
28+
if (argc < 3) {
29+
fprintf(stderr, "Usage: ./test value n1 n2 n3...\n");
30+
exit(-1);
31+
}
32+
33+
int value = atoi(argv[1]);
34+
int i, count = argc - 2;
35+
struct ListNode *head = NULL, *p, *prev;
36+
for (i = 0; i < count; i++) {
37+
p = malloc(sizeof(*p));
38+
p->val = atoi(argv[i + 2]);
39+
p->next = NULL;
40+
if (head == NULL) {
41+
head = p;
42+
} else {
43+
prev->next = p;
44+
}
45+
prev = p;
46+
}
47+
48+
for (p = removeElement(head, value); p != NULL; p = p->next) {
49+
printf("%d ", p->val);
50+
}
51+
printf("\n");
52+
return 0;
53+
}

204_count_primes/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test count_primes.c

204_count_primes/count_primes.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
static int countPrimes(int n)
7+
{
8+
if (n < 3) return 0;
9+
10+
int i, j;
11+
bool *marked = malloc(n);
12+
memset(marked, false, n);
13+
int count = n >> 1;
14+
15+
for (i = 3; i * i <= n; i += 2) {
16+
if (!marked[i]) {
17+
for (j = i * i; j < n; j += (i << 1)) {
18+
if (!marked[j]) {
19+
marked[j] = true;
20+
--count;
21+
}
22+
}
23+
}
24+
}
25+
return count;
26+
}
27+
28+
int main(int argc, char **argv)
29+
{
30+
if (argc != 2) {
31+
fprintf(stderr, "Usage: ./test n\n");
32+
exit(-1);
33+
}
34+
35+
printf("%d\n", countPrimes(atoi(argv[1])));
36+
return 0;
37+
}

205_isomorphic_strings/1

Whitespace-only changes.

205_isomorphic_strings/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test isomorphic_strings.c
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
static bool isIsomorphic(char *s, char *t)
6+
{
7+
int i;
8+
char smap[128] = { '\0' };
9+
char tmap[128] = { '\0' };
10+
while (*s != '\0' && *t != '\0') {
11+
if ((smap[*s] != '\0' && smap[*s] != *t) ||
12+
(tmap[*t] != '\0' && tmap[*t] != *s)) {
13+
return false;
14+
}
15+
smap[*s] = *t;
16+
tmap[*t] = *s;
17+
s++;
18+
t++;
19+
}
20+
return *s == *t;
21+
}
22+
23+
int main(int argc, char **argv)
24+
{
25+
if (argc != 3) {
26+
fprintf(stderr, "Usage: ./test s1 s2\n");
27+
exit(-1);
28+
}
29+
30+
printf("%s\n", isIsomorphic(argv[1], argv[2]) ? "true" : "false");
31+
return 0;
32+
}

206_reverse_linked_list/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test reverse_list.c

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy