Przeglądaj źródła

Another May Problem done

master
Lachlan Jacob 5 lat temu
rodzic
commit
b0ce186452
3 zmienionych plików z 46 dodań i 0 usunięć
  1. 16
    0
      problems/476/main.c
  2. 25
    0
      problems/476/problem.txt
  3. 5
    0
      problems/476/run.sh

+ 16
- 0
problems/476/main.c Wyświetl plik

@@ -0,0 +1,16 @@
#include <stdio.h>

int findComplement(int num) {
int mask = num;
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
return ~num & mask;
}

int main() {
printf("Expected: 2\n");
printf("Got: %d\n", findComplement(5));
}

+ 25
- 0
problems/476/problem.txt Wyświetl plik

@@ -0,0 +1,25 @@
Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.


Example 1:

Input: num = 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: num = 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


Constraints:

The given integer num is guaranteed to fit within the range of a 32-bit signed integer.
num >= 1
You could assume no leading zero bit in the integer’s binary representation.
This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/


+ 5
- 0
problems/476/run.sh Wyświetl plik

@@ -0,0 +1,5 @@
#!/bin/bash

gcc -o main main.c
./main
rm main

Ładowanie…
Anuluj
Zapisz