【C++---6】STL的简单应用


两个栈实现队列:

题目描述:

使用栈实现队列的下列操作:

push(x) – 将一个元素放入队列的尾部
pop() – 从队列首部移除元素
peek() – 返回队列首部的元素
empty() – 返回队列是否为空

示例:

MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

代码示例:

把c语言写法变成C++写法:

class MyQueue 
{
public:
	    stack<int> s1;
		stack<int> s2;
    
	    /** Initialize your data structure here. */
	    MyQueue() 
	    {
        
	    }
    
	    /** Push element x to the back of queue. */
	    void push(int x) 
	    {
	        s1.push(x);
	    }
    
	    /** Removes the element from in front of queue and returns that element. */
	    int pop() 
	    {
	        while (s1.empty() == false)
			{
				s2.push(s1.top());
				s1.pop();
			}
			int a= s2.top();

			s2.pop();
			while (s2.empty() == false)
			{
				s1.push(s2.top());
				s2.pop();
			}

			return a;
	    }
    
	    /** Get the front element. */
	    int peek() 
	    {
	        while (s1.empty() == false)
			{
				s2.push(s1.top());
				s1.pop();
			}
			int a= s2.top();

			while (s2.empty() == false)
			{
				s1.push(s2.top());
				s2.pop();
			}

			return a;
	    }
	    
	    /** Returns whether the queue is empty. */
	    bool empty() 
	    {
	        if (s1.empty() == true && s2.empty() == true)
			{
				return true;
			}
			return false;
	    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

用队列实现栈:

题目描述:

使用队列实现栈的下列操作:

push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空

代码示例:

和上面一样的方法

class MyStack 
{
public:
	    queue<int> q1;
	    queue<int> q2;
    
	    /** Initialize your data structure here. */
	    MyStack() 
	    {
        
	    }
    
	    /** Push element x onto stack. */
	    void push(int x) 
	    {
	        q1.push(x);
	    }
	    
	    /** Removes the element on top of the stack and returns that element. */
	    int pop() 
	    {
	        while(q1.size()>1)
	        {
	            q2.push(q1.front());
	            q1.pop();
	        }
	        int a=q1.front();
		        
	        q1.pop();
        
	        while(q2.empty()==false)
	        {
	            q1.push(q2.front());
	            q2.pop();
	        }
	        return a;
	    }
    
	    /** Get the top element. */
	    int top() 
	    {
	        return q1.back();
	    }
    
	    /** Returns whether the stack is empty. */
	    bool empty() 
	    {
	        if(q1.empty()==true && q2.empty()==true)
	        {
	            return true;
	        }
	        return false;
	    }
};

/**
 * 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();
 */

找出只出现一次的数:

题目描述:

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素

示例 1:

输入: [2,2,3,2]
输出: 3

示例 2:

输入: [0,1,0,1,0,1,99]
输出: 99

思路分析:

0 ^ x = x,

x ^ x = 0;

x & ~x = 0,

x & ~0 =x;

<1>.定义两个变量a和b,初始化为0;
<2>.当x这个数出现第一次的时候a=(a^x) & (~b),结果为 x& ~0为x;b=(b^x) & (~a),b为0
<3>.当x这个数出现第二次的时候a=(a^x) & (~b),结果为 0& ~0为0;b=(b^x) & (~a),b为x
<4>.当x这个数出现第三次的时候a=(a^x) & (~b),结果为 x& ~x为0;b=(b^x) & (~a),b为0 & ~0为0

代码示例:

class Solution 
{
public:
	    int singleNumber(vector<int>& nums) 
	    {
	        int a=0,b=0;
	        for(int i=0;i<nums.size();++i)
	        {
	            a = (a^nums[i]) & ~b;
	            b = (b^nums[i]) & ~a;
	        }
	        return a;
	    }
};

删除排序数组中的重复项:

题目描述:

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成

示例 1:

给定数组 nums = [1,1,2],

函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2

你不需要考虑数组中超出新长度后面的元素

示例 2:

给定 nums = [0,0,1,1,1,2,2,3,3,4],

函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4

你不需要考虑数组中超出新长度后面的元素

思路分析:

<1>.极端情况就是有效元素个数0或者只有1个,直接返回数组本身的size
<2>.创建一个循环,从1号元素开始遍历,如果0号位置的元素与1号位置元素不等,就说明不重复
<3>.不重复的话就把1号元素还是放在1号位置.a必须前置++
<4>.a如果后置++会导致数组打印出来顺序相反

这个方法只适合数组有序!!! 否则后面的元素与前面的重复(并不与该元素前一位重复)处理不了~~

代码示例:

class Solution 
{
public:
	    int removeDuplicates(vector<int>& nums) 
	    {       
	        if(nums.size()<=1)
	        {
	            return nums.size();
	        }
        
	        int a=0;
        
	        for(int i=1;i<nums.size();++i)
	        {
	            if(nums[a]!=nums[i])
	            {
	                nums[++a]=nums[i];
	            }
	        }
        
	        //a是下标,返回的长度+1
	        return a+1;
	    }
};

二叉树的层序遍历:

题目描述:

给定一个二叉树,返回其按层次遍历的节点值 (即逐层地,从左到右访问所有节点)

例如:

给定二叉树: [3,9,20,null,null,15,7],

	    3
	   / \
	  9  20
	    /  \
	   15   7

返回其层次遍历结果:

	[
	  [3],
	  [9,20],
	  [15,7]
	]

代码示例:

/**
 * 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:
	    vector<vector<int>> levelOrder(TreeNode* root) 
	    {
	        vector<vector<int>> vv;
	        
	        if(root==nullptr)
	        {
	            return vv;
	        }
        
	        queue<TreeNode*> q;
        
	        q.push(root);
        
	        while(q.empty()==false)
	        {
	            //层序遍历出的节点的个数就是队列的有效元素
	            int levelcount=q.size();
            
	            vector<int> level;
            
	            for(int i=0;i<levelcount;++i)
	            {
	                //创建一个指针从根节点开始
	                TreeNode* pCur=q.front();
	                level.push_back(pCur->val);
            
	                if(pCur->left!=nullptr)
	                {
	                    q.push(pCur->left);
	                }
              
	                if(pCur->right!=nullptr)
	                {
		                q.push(pCur->right);
	                }   
            
	                q.pop();
	            }
            
	            vv.push_back(level);
	        }
	        return vv;
	    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值