leetcode解题思路分析(八十)700 - 707 题

  1. 二叉搜索树中的搜索
    给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。

知道二叉搜索树的性质直接写就完事

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    TreeNode* searchBST(TreeNode* root, int val) 
    {
    
    
        TreeNode* ptr = root;

        while (ptr != NULL)
        {
    
    
            if (ptr->val == val)
            {
    
    
                return ptr;
            }
            else if (ptr->val < val)
            {
    
    
                ptr = ptr->right;
            }
            else if (ptr->val > val)
            {
    
    
                ptr = ptr->left;
            }
        }

        return NULL;
    }
};
  1. 二叉搜索树中的插入操作
    给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。

迭代找就行了

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) 
    {
    
    
        TreeNode* ptr = root;

        if (root == NULL)
        {
    
    
            root = new TreeNode(val);
            return root;
        }

        while (ptr)
        {
    
    
            if (ptr->val < val)
            {
    
    
                if (ptr->right)
                {
    
    
                    ptr = ptr->right;
                    continue;
                }
                else
                {
    
    
                    ptr->right = new TreeNode(val);
                    break;
                }
            }
            else if (ptr->val > val)
            {
    
    
                if (ptr->left)
                {
    
    
                    ptr = ptr->left;
                    continue;
                }
                else
                {
    
    
                    ptr->left = new TreeNode(val);
                    break;                    
                }
            }
        }

        return root;

    }
};
  1. 数据流中的第 K 大元素
    设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。

堆的简单使用

class KthLargest 
{
    
    
    int size = 0;
    priority_queue< int, vector<int>, greater<int> > m_heap;
public:
    KthLargest(int k, vector<int>& nums) 
    {
    
    
        size = k;
        for (auto &n : nums)
        {
    
    
            add(n);
        }
    }
    
    int add(int val) 
    {
    
    
        m_heap.push(val);
        if (m_heap.size() > size)
        {
    
    
            m_heap.pop();
        }
        return m_heap.top();       
    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

  1. 二分查找
    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。

经典二分查找题

class Solution {
    
    
public:
    int search(vector<int>& nums, int target) 
    {
    
    
        int nLeft = 0, nRight = nums.size() - 1, nMid = 0;

        while (nLeft <= nRight)
        {
    
    
            nMid = nLeft + (nRight - nLeft) / 2;

            if (nums[nMid] == target) 
            {
    
    
                return nMid;
            }
            else if (nums[nMid] > target)
            {
    
    
                nRight = nMid - 1;
            }
            else if (nums[nMid] < target)
            {
    
    
                nLeft = nMid + 1;
            }            
        }

        return -1;
    }
};
  1. 设计哈希集合
    不使用任何内建的哈希表库设计一个哈希集合(HashSet)。
class MyHashSet {
    
    
private:
    vector<list<int>> data;
    static const int base = 769;
    static int hash(int key) {
    
    
        return key % base;
    }
public:
    /** Initialize your data structure here. */
    MyHashSet(): data(base) {
    
    }
    
    void add(int key) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it) == key) {
    
    
                return;
            }
        }
        data[h].push_back(key);
    }
    
    void remove(int key) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it) == key) {
    
    
                data[h].erase(it);
                return;
            }
        }
    }
    
    /** Returns true if this set contains the specified element */
    bool contains(int key) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it) == key) {
    
    
                return true;
            }
        }
        return false;
    }
};

/**
 * Your MyHashSet object will be instantiated and called as such:
 * MyHashSet* obj = new MyHashSet();
 * obj->add(key);
 * obj->remove(key);
 * bool param_3 = obj->contains(key);
 */
  1. 设计哈希映射
class MyHashMap {
    
    
private:
    vector<list<pair<int, int>>> data;
    static const int base = 769;
    static int hash(int key) {
    
    
        return key % base;
    }
public:
    /** Initialize your data structure here. */
    MyHashMap(): data(base) {
    
    }
    
    /** value will always be non-negative. */
    void put(int key, int value) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it).first == key) {
    
    
                (*it).second = value;
                return;
            }
        }
        data[h].push_back(make_pair(key, value));
    }
    
    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    int get(int key) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it).first == key) {
    
    
                return (*it).second;
            }
        }
        return -1;
    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) {
    
    
        int h = hash(key);
        for (auto it = data[h].begin(); it != data[h].end(); it++) {
    
    
            if ((*it).first == key) {
    
    
                data[h].erase(it);
                return;
            }
        }
    }
};


  1. 设计链表
class MyLinkedList {
    
    
public:
    //定义链表节点结构体
    struct LinkedNode
    {
    
    
        int val;
        LinkedNode *next;
        LinkedNode(int val):val(val),next(nullptr){
    
    }
    };
    //初始化链表
    MyLinkedList() {
    
    
        m_dummyHead=new LinkedNode(0);
        m_size=0;
    }
    //获取到第index个节点数值,如果index是非法数值直接返回-1,注意index是从0开始的,第0个节点就是头结点
    int get(int index) {
    
    
        if(index>m_size-1||index<0)
        {
    
    
            return -1;
        }
        LinkedNode *cur=m_dummyHead->next;
        while(index--)//注意--index会陷入死循环
        {
    
    
            cur=cur->next;
        }
        return cur->val;
    }
    //在链表最前面插入一个结点,插入完成后,新插入的结点为链表的新的头结点
    void addAtHead(int val) {
    
    
        LinkedNode *newNode=new LinkedNode(val);
        LinkedNode *cur=m_dummyHead;
        newNode->next=cur->next;
        cur->next=newNode;
        m_size++;//注意++
    }
    //在链表最后面添加一个结点
    void addAtTail(int val) {
    
    
        LinkedNode *newNode=new LinkedNode(val);
        LinkedNode *cur=m_dummyHead;
        while(cur->next!=NULL)
        {
    
    
            cur=cur->next;
        }
        cur->next=newNode;
        m_size++;//注意++
    }
    //在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头结点
    //如果index等于链表的长度,则说明是新插入和结点为链表的尾结点
    //如果index大于链表的长度,则返回空
    void addAtIndex(int index, int val) {
    
    
        if(index>m_size)
        {
    
    
            return;
        }
        LinkedNode *newNode=new LinkedNode(val);
        LinkedNode *cur=m_dummyHead;
        while(index--)
        {
    
    
            cur=cur->next;
        }
        newNode->next=cur->next;
        cur->next=newNode;
        m_size++;//注意++
    }
    //删除第index个节点,如果index大于等于链表的长度,直接return,注意index是从0开始的
    void deleteAtIndex(int index) {
    
    
        if(index>=m_size||index<0)
        {
    
    
            return;
        }
        LinkedNode *cur=m_dummyHead;
        while(index--)
        {
    
    
            cur=cur->next;
        }
        LinkedNode *tmp=cur->next;
        cur->next=cur->next->next;
        delete tmp;
        m_size--;//注意--
    }
    private:
    int m_size;
    LinkedNode *m_dummyHead;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */


猜你喜欢

转载自blog.csdn.net/u013354486/article/details/118068288