leecode 1:两数之和、27:移除元素、100:相同的树

两数之和
在这里插入图片描述

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int>tmp;
        for(int i=0;i<nums.size();++i)
        {    
            tmp.push_back(i);
            for(int j=i+1;j<nums.size();++j)
            {
                if(target==nums[j]+nums[i])
                {
                    tmp.push_back(j);
                    return tmp;
                }
            }
            tmp.pop_back();
        }
        return tmp;
    }
};

在这里插入图片描述
法2:哈希表

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int>tmp;
        unordered_map<int ,int >hash_map;
        for(int i=0;i<nums.size();i++)
        {
            hash_map.insert({nums[i],i});

        }
        for(int j=0;j<nums.size();++j)
        {
            tmp.push_back(j);
            
               int x=target-nums[j];
               if(hash_map.count(x)&&hash_map.at(x)!=j){
                tmp.push_back(hash_map.at(x));
                return tmp;
               
               }
            tmp.pop_back();
        }
        return tmp;
    }
};

12ms, 内存11mb


移除元素

在这里插入图片描述

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        ///sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size();++i)
        {
            if(nums[i]==val)
            {
                nums.erase(nums.begin()+i);
                i--;//元素移除后i的值需要调整。
            }
        }
        return nums.size();
    }
};

很显然,如果删除的元素在数组中是n个,时间复杂度是0(N^2),空间复杂度为O(1)。本题如果要用O(n)方法去做,可以用快慢指针的方法:
快慢指针

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int slowIndex = 0;
        for (int fastIndex = 0; fastIndex < nums.size(); fastIndex++) {
            if (val != nums[fastIndex]) {
                nums[slowIndex++] = nums[fastIndex];
            }
        }
        return slowIndex;
    }
};

这样就实现了一遍遍历删除所有目标元素。

原文连接(有附图):
https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html#%E6%80%9D%E8%B7%AF


在这里插入图片描述

/**
 * 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:
    bool isSameTree(TreeNode* p, TreeNode* q)
    {
        if(p==nullptr&&q==nullptr){return true;}
        if((p!=nullptr&&q!=nullptr)&&(q->val!=p->val)){return false;}
        if(q!=nullptr&&p==nullptr){return false;}
        if(q==nullptr&&p!=nullptr){return false;}
       bool tag= isSameTree(p->left, q->left);
        bool tag1=isSameTree(p->right, q->right);
        return  tag&&tag1;
    }
        
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值