[日常刷题]leetcode D21

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/82912133

219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false

Solution in C++:

关键点:

  • at most k 带等号

思路:

  • 跟上次刷的最后一题类似,使用map,key用来数,value用来存储出现的下标。如果没有遇到过的key,加入,如果遇到过的判断下标差是否大于k,不大于即返回true,大于之后就更新现在的value为i,继续判断后面的值。
bool containsNearbyDuplicate(vector<int>& nums, int k) {
        
        map<int,int> maps;
        map<int,int>::iterator it;
        size_t size = nums.size();
        
        for(int i = 0; i < size; ++i)
        {
            it = maps.find(nums[i]);
            if( it == maps.end())          // 第一次碰到数字nums[i]
            {
                maps[nums[i]] = i;
            } else{                        // 不是第一次碰到
                if(maps[nums[i]] + k >= i)  // 且二者差距小于k
                    return true;
                else
                    maps[nums[i]] = i;
            }
        }
        
        return false;
    }

225. Implement Stack using Queues

Implement the following operations of a stack using queues.

  • push(x) – Push element x onto stack.
  • pop() – Removes the element on top of the stack.
  • top() – Get the top element.
  • empty() – Return whether the stack is empty.

Example:

MyStack stack = new MyStack();

stack.push(1);
stack.push(2);  
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false

Notes:

  • You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is emptyoperations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Solution in C++:

关键点:

  • stack FILO queue FIFO

思路:

  • 开始看到这个,打算用单纯的queue来实现,但是后来直接简化用deque来实现了。基本没什么难度了。
  • 然后看了解析,里面有几个想法,我大致说一下主要分成两大类
  • two queues :
    • 1.一个正常使用,另外一个在pop操作的时候打辅助。把除了最后一个元素外的所有元素从一个转到另一个,之后弹出最后一个元素,将两个队列交换
    • 2:.另外一个方法就是在push的时候借助第二个队列,使得存入在第一个队列中的顺序为原来的逆序
  • one queue:和两个队列的第二个方法类似,将队列中的元素逆序。
class MyStack {
public:
    deque<int> mydeque;
    deque<int>::iterator it;
    /** Initialize your data structure here. */
    MyStack() {
        it = mydeque.begin();
    }
    
    /** Push element x onto stack. */
    void push(int x) {
       mydeque.push_back(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int result = mydeque.back();
        mydeque.pop_back();
        return result;
    }
    
    /** Get the top element. */
    int top() {
        return mydeque.back();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return mydeque.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */

226. Invert Binary Tree

Invert a binary tree.
Example:
Input:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.

Solution in C++:

关键点:

  • 左右交换

思路:

  • 递归
    获得交换后的左子树及右子树后,root->left = right; root->right = left即可
  • 迭代
    需要借助队列来完成,就像层次遍历一样,需要把每层的结点记住,每个结点的左右子树进行交换。

方法一:递归

TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr)
            return root;
        
        TreeNode *left = invertTree(root->left);
        TreeNode *right = invertTree(root->right);
        root->left = right;
        root->right = left;
        return root;
    }

方法二:迭代

TreeNode* invertTree(TreeNode* root)
    {
        if (root == nullptr)
            return root;
        
        queue<TreeNode*> nodes;
        nodes.push(root);
        
        while(!nodes.empty())
        {
           TreeNode* cur = nodes.front();
           nodes.pop();
           TreeNode* tmp = cur->left;
           cur->left = cur->right;
           cur->right = tmp;
           if (cur->left != nullptr) nodes.push(cur->left);
           if (cur->right != nullptr) nodes.push(cur->right); 
        }
        return root;
    }

小结

今天主要练习了一些容器的使用方法。以及一些数据结构的复习。

知识点

  • map、queue、deque用法
  • stack、queue性质
  • 树的反转(递归及迭代方法)

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/82912133