# [C] Move a range of bits to a new range in a number

Description:

`void move_bits(int *a, int l, int new_l, int num){    if(num == 0)        return;    //create get_mask    int get_mask = 0;    int  …`

# [Leetcode 20] Valid Parentheses

Description:

Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.

Example…

# [Leetcode 693] Binary Number with Alternating Bits

Description:

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

Example 1:

`Input: n = 5Output: trueExplanation: The binary representation of 5 is: 101`

Example 2:

`Input: n = 7Output: falseExplanation: The binary representation of 7 is: 111.`

Example 3:

`Input: n = 11Output: falseExplanation: The binary representation of 11 is: 1011.`

Example 4:

`Input: n = 10Output: trueExplanation: The binary representation of 10 is: 1010.`

Example 5:

`Input: n = 3Output: false`

Constraints:

• `1 <= n <= 231 - 1`

C code solution:

`bool hasAlternatingBits(int n){    int prev = n&1;    n = n >> 1;        while(n){        if(prev == (n&1)){            return false;        }                    prev = n & 1;        n = n >> 1;    }    return true;}`

# [Leetcode 617] Merge Two Binary Trees

Description:

You are given two binary trees `root1` and `root2`.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule…

# [Leetcode 190] Reverse Bits

Description:

Reverse bits of a given 32 bits unsigned integer.

Example 1:

`Input: n = 00000010100101000001111010011100Output:    964176192 (00111001011110000010100101000000)Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.`

Example 2:

`Input: n = 11111111111111111111111111111101Output:   3221225471 (10111111111111111111111111111111)Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.`

Constraints:

• The input must be a binary string of length `32`

C code solution:

`uint32_t reverseBits(uint32_t n) {    int hash[32] = {0};    int i = 0;    // get the status of each digits into hash    while(n){        if(n&1 == 1)            hash[i] = 1;        i++;        n = n >> 1;    }    //reform the new number inversely    uint32_t res = 0;    for(i=31; i>=0; i--){        res += pow(2, 31-i) * hash[i];    }    return res;}`

Insertion Sort:

# [Leetcode 700] Search in a Binary Search Tree

Description:

You are given the `root` of a binary search tree (BST) and an integer `val`.

Find the node in the BST that the node’s value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.

Example 1:

`Input: root =…`

# [Leetcode 701] Insert into a Binary Search Tree

Description:

You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

Notice that there may exist…

## Samuel Liu

Hi I am Samuel

Get the Medium app