[Leetcode]27. Remove Element

版权声明:转载请联系博主 https://blog.csdn.net/zxtalentwolf/article/details/84335324

使用库函数upper,lower可以很方便的找到对应的位置删去就好了

坑点:

1.空数组

2.删去的值有可能不在数组里面 

const int x=[]{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return 0;
}();
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        sort(nums.begin(),nums.end());
        auto s = lower_bound(nums.begin(), nums.end(),val);
        if(s == nums.end() || *s != val) return nums.size();
        auto t = upper_bound(nums.begin(), nums.end(),val);
        nums.erase(s,t);
        return nums.size();
        
    }
};

猜你喜欢

转载自blog.csdn.net/zxtalentwolf/article/details/84335324