Skip to content

Commit ca2e846

Browse files
2 parents 76bb9cb + 621e77c commit ca2e846

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

189_rotate_array/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 rotate_array.c

189_rotate_array/rotate_array.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static void reverse(int *nums, int lo, int hi)
5+
{
6+
while (lo < hi) {
7+
int tmp = nums[lo];
8+
nums[lo] = nums[hi];
9+
nums[hi] = tmp;
10+
lo++;
11+
hi--;
12+
}
13+
}
14+
15+
static void rotate(int* nums, int numsSize, int k)
16+
{
17+
k %= numsSize;
18+
if (k == 0) {
19+
return;
20+
}
21+
22+
reverse(nums, 0, numsSize - 1 - k);
23+
reverse(nums, numsSize - k, numsSize - 1);
24+
reverse(nums, 0, numsSize - 1);
25+
}
26+
27+
int main(int argc, char **argv)
28+
{
29+
if (argc < 2) {
30+
fprintf(stderr, "Usage: ./test k n1 n2...\n");
31+
exit(-1);
32+
}
33+
34+
int k = atoi(argv[1]);
35+
int i, count = argc - 2;
36+
int *nums = malloc(count * sizeof(int));
37+
for (i = 0; i < count; i++) {
38+
nums[i] = atoi(argv[i + 2]);
39+
}
40+
41+
rotate(nums, count, k);
42+
43+
for (i = 0; i < count; i++) {
44+
printf("%d ", nums[i]);
45+
}
46+
printf("\n");
47+
48+
return 0;
49+
}

190_reverse_bits/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_bits.c

190_reverse_bits/reverse_bits.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
5+
static uint32_t reverseBits(uint32_t n)
6+
{
7+
int i;
8+
uint32_t res = 0;
9+
for (i = 0; i < 32; i++) {
10+
res <<= 1;
11+
res |= n & 0x1;
12+
n >>= 1;
13+
}
14+
return res;
15+
}
16+
17+
int main(int argc, char **argv)
18+
{
19+
if (argc != 2) {
20+
fprintf(stderr, "Usage: ./test n\n");
21+
exit(-1);
22+
}
23+
24+
printf("%u\n", reverseBits(atoi(argv[1])));
25+
return 0;
26+
}

191_number_of_one_bits/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 hamming_weight.c
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
5+
static int hammingWeight(uint32_t n)
6+
{
7+
int count = 0;
8+
while (n > 0) {
9+
n = n & (n - 1);
10+
count++;
11+
}
12+
return count;
13+
}
14+
15+
int main(int argc, char **argv)
16+
{
17+
if (argc != 2) {
18+
fprintf(stderr, "Usage: ./test n\n");
19+
exit(-1);
20+
}
21+
22+
printf("%d\n", hammingWeight(atoi(argv[1])));
23+
return 0;
24+
}

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