Prepare for the second test, three questions a day, Day26

Posted by orion2004 on Thu, 03 Feb 2022 06:45:36 +0100

Prepare for the second test, three questions a day

Topic 1: square root of x

Give you a nonnegative integer x, calculate and return the arithmetic square root of X.

Since the return type is an integer, only the integer part will be retained and the decimal part will be rounded off.

Note: it is not allowed to use any built-in exponential functions and operators, such as pow(x, 0.5) or x ** 0.5.

Example 1:

Input: x = 4
Output: 2
Example 2:

Input: x = 8
Output: 2
Explanation: the arithmetic square root of 8 is 2.82842... Because the return type is an integer, the decimal part will be rounded off.

Tips:

0 <= x <= 231 - 1

Source: LeetCode
Link: https://leetcode-cn.com/problems/sqrtx

class Solution {
public:
    int mySqrt(int x) {
        long count=1;
        if(x==0) return 0;
        while(count<=x){
            if(count*count==x){
                break;
            }else if(count*count>x){
                count--;
                break;
            }
            count++;
        }
        return count;

    }
};

Topic 2: there are duplicate elements

Give you an integer array nums. If any value appears in the array at least twice, return true; If each element in the array is different from each other, false is returned.

Example 1:

Input: num = [1,2,3,1]
Output: true
Example 2:

Input: num = [1,2,3,4]
Output: false
Example 3:

Input: num = [1,1,1,3,3,4,3,2,4,2]
Output: true

Tips:

1 <= nums.length <= 105
-109 <= nums[i] <= 109

Source: LeetCode
Link: https://leetcode-cn.com/problems/contains-duplicate

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size()-1;i++){
            if(nums[i]==nums[i+1]) return true;
        }
        return false;

    }
};

Topic 3: the nearest common ancestor of binary tree

Given a binary tree, find the nearest common ancestor of two specified nodes in the tree.

Baidu Encyclopedia defines the nearest public ancestor as: "for two nodes p and q with root tree T, the nearest public ancestor is represented as a node x, which satisfies that x is the ancestor of p and q, and the depth of X is as large as possible (a node can also be its own ancestor)."

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: the nearest common ancestor of node 5 and node 1 is node 3.
Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: the nearest common ancestor of node 5 and node 4 is node 5. Because according to the definition, the nearest common ancestor node can be the node itself.
Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

Tips:

The number of nodes in the tree is in the range [2, 105].
-109 <= Node.val <= 109
All nodes They are different from each other.
p != q
Both p and q exist in a given binary tree.
Pass times 305023 submit times 445652

Source: LeetCode
Link: https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root==NULL||p==root||q==root) return root;
        //Postorder traversal framework
        //Because we want to find the nearest common ancestor, we should look from bottom to top, that is, we should use backtracking to look for it
        TreeNode* left=lowestCommonAncestor(root->left,p,q);
        TreeNode* right=lowestCommonAncestor(root->right,p,q);
        
        if(left!=NULL&&right!=NULL){
            return root;
        }else if(left==NULL&&right!=NULL){
            return right;
        }else if(left!=NULL&&right==NULL){
            return left;
        }else{
            return NULL;
        }
    }
};

Topics: Algorithm data structure leetcode