Summary of 2021-10-22 Brushing Titles

Posted by stlewis on Thu, 21 Oct 2021 19:19:21 +0200

Catalog

Summary of Today's Brush Topics

Single-valued binary tree (easy)

The same tree (easy)

Prime number calculation position (easy) in binary representation

Number (mid)II that occurs only once

supplement

Summary of Today's Brush Topics

Single-valued binary tree (easy)

Title link: 965.Single Value Binary Tree-Force Button (leetcode-cn.com)

Title Description

If each node of a binary tree has the same value, the binary tree is a single-valued binary tree.

Returns true only if the given tree is a single-valued binary tree. Otherwise, it returns false.

Example

Solving problems

Recursive traversal: Completes a recursive traversal using a Boolean type auxiliary function. In the main function, the root node value root.val and the root node root are passed into the auxiliary function to allow the auxiliary function to complete the recursive comparison.

Variable: Auxiliary function parameter An integer val ue A node TreeNode.

Code

class Solution {
    
    public boolean isUnivalTree(TreeNode root) {
​
        if(root.left == null && root.right == null){
​
            return true;
        }
​
        int val = root.val;
​
        return singleNumber(val,root);
    }
    public boolean singleNumberTree( int val, TreeNode root){
​
        if(root == null){
​
            return true;
        }
​
        if(root.val != val){
            
            return false;
        }
​
        return singleNumber(val,root.left) && singleNumber(val,root.right);
    }
}

The same tree (easy)

Title link: 100.Same tree-snap (LeetCode) (leetcode-cn.com)

Title Description

Give you the root nodes p and q of two binary trees and write a function to check if they are the same.

If two trees are structurally identical and nodes have the same value, they are considered identical.

Example

 

Solving problems

Recursive comparison: The idea is very simple. Traverse through each node of two trees to compare values. One node is empty, the other is empty.

Omnipotent recursion, Ha Ha Ha Ha Ha Ha has come to water again.

Code

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
​
        if(p == null){
​
            return q == null;
​
        }
        if(q == null){
​
            return p == null;
            
        }
        if(p.val != q.val ){
​
            return false;
​
        }
​
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}

Prime number calculation position (easy) in binary representation

Title link: Prime number calculation position in binary representation

Title Description

Given two integers L and R, find the range of closed intervals [L, R], and calculate the number of integers with Prime digits.

(Note that computed bits represent the number of 1 in a binary representation. For example, binary representation 10101 of 21 has three computed bits. Also, 1 is not a prime number.)

Example

Input: L = 10, R = 15

Output: 5

Explanation:

10 -> 1010 (2 computed bits, 2 is prime)

11 -> 1011 (3 computed bits, 3 is prime)

12 -> 1100 (2 computed bits, 2 is prime)

13 -> 1101 (3 computed bits, 3 is prime)

14 -> 1110 (3 computed bits, 3 is prime)

15 -> 1111 (4 computed bits, 4 not prime)

Be careful

  1. L, R is an integer L <= R in [1, 10^6].

  2. The maximum value of R - L is 10000.

     

Solving problems

Enumeration+Bit Operation:

  • Enumeration: Since the range of L and R in this topic is within 10 ^ 6, the binary integer representation of R does not exceed 20 bits (20 times of 2 is 1048576), so the maximum prime number in the number of digits is 19 and the amount of data is small, you can consider enumerating prime numbers in 2 - 20.

  • Bit operation: Yesterday's article on the use of bits has a quick solution to the number of binary bits in a number, namely lowbit.

    I don't know a buddy. I can go and see that. The link is here.

    Smart bitwise operations_

Code

class Solution {
    public int countPrimeSetBits( int left, int right) {
        int count = 0;
        while( left <= right ) {
            int sum = 0;
            int m = left;
            while(m != 0){
                int c = m & ( -m);
                m -= c;
                sum ++;
                
            }
            left ++;
            count = check(sum) ? count + 1 : count;
        }
        return count;
    }
    public boolean check(int num){
        return num == 2 || num == 3 || num == 5 || num == 7 || num == 11 ||num == 13||num == 17||num == 19;
    }
}

Number (mid)II that occurs only once

Title link: Number II - Submit Record - LeetCode (leetcode-cn.com) that occurs only once

Title Description

Give you an array of integers, nums, where each element occurs exactly three times, except for one that occurs only once. Please find out and return the element that only appears once.

Example

​
Input: nums = [2,2,3,2]
Output: 3
​
Input: nums = [0,1,0,1,0,1,99]
Output: 99
​

Solving problems

Hashtable: Traverse through the array, creating a hash table to record the number of occurrences of each number, returning an occurrence of 1 element.

Bit operation: We know that if two numbers are not equal, then they must have some binary digits that are not the same, so we can record the number of occurrences of 1 in the k-bit binary number of all the numbers in the array. If the number occurs three times in the array, then count must be divisible by 3, otherwise, the number that occurs once must be 1 in the k-bit. We just need to record every one of these k-bits and get every   Add 1 << K.

Bit Operations Article: Smart bitwise operations_

Code

class Solution {
    public int singleNumber(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        }
        int res = 0;
        for(int i =0;i < 32 ;i ++){
            int count = 0;
            for(int item : nums){
                if(( item & 1 << i) == 1<<i){
                    count ++;
                }
            }
            if(count %3 == 1){
                res |= (1 << i);
            }
        }
        return res;
    }
}

supplement

Today, I watched Queen N and its optimization problems, which is really a beginner's nightmare, but after reading, I also understood some recursive characteristics, Cheese Cheese!!!

Okay, let's get here today (another day on water) and continue to feed the ducks tomorrow.

Topics: Algorithm leetcode