麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:27 Remove Element

2019-11-14 12:58:08
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>#include <algorithm>using namespace std;/*問題:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The order of elements can be changed. It doesn't matter what you leave beyond the new length.Example:Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.分析:此題實際上就是刪除元素為指定值的元素,數組事先是無序的。一種簡單方法是:先排序,排序結束后通過二分查找元素上下區間,刪除區間內元素,時間復雜度為O(N*logN)如果不排序,刪除指定元素,每次從前向后移動,找到指定元素后,將該元素后面所有元素向前移動一位,遍歷元素的時間復雜度為O(N),找到元素后,后面所有元素向前移動時間復雜度為O(N),總的時間復雜度為O(N*N)lower_bound:查找元素第一個出現的位置,或者如果沒有該元素,查找大于該元素的位置,使得將元素插入數組后數組仍然有序upper_bound:查找大于元素x的第一個位置,使得如果將待查找元素插入在該位置后,該數組仍然有序輸入:4(元素個數) 3(待刪除元素)3 2 2 3(所有數組元素)4 13 2 2 3輸出:2(新數組的長度)4*/void PRint(vector<int>& nums){if(nums.empty()){cout << "nums is empty" << endl;return ;}int length = nums.size();for(int i = 0 ; i < length ; i++){cout << nums.at(i) << " ";}cout << endl;}class Solution {public:    int removeElement(vector<int>& nums, int val) {if(nums.empty()){return 0;}//排序sort(nums.begin() , nums.end());//查找>=元素的第一個出現的位置,位置是迭代器vector<int>::iterator low = lower_bound(nums.begin() , nums.end() , val);//超找>元素第一個出現的位置vector<int>::iterator high = upper_bound(nums.begin() , nums.end() , val);//刪除元素nums.erase(low , high);int length = nums.size();//print(nums);return length;    }};void process(){int num;vector<int> datas;int value;int delVal;while(cin >> num >> delVal){datas.clear();for(int i = 0 ; i < num ; i++){cin >> value;datas.push_back(value);}Solution solution;int result = solution.removeElement(datas , delVal);cout << result << endl;}}int main(int argc , char* argv[]){process();getchar();return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品国产一区二区三区四 | 色妞妞视频 | 久久精品免费国产 | 一级黄色影院 | chinesexxxx刘婷hd 国产资源视频在线观看 | av在线免费观看网址 | 日本欧美一区二区三区在线观看 | 九九黄色 | 欧美日韩免费看 | 全黄毛片 | 欧美日韩国产成人在线观看 | fc2成人免费人成在线观看播放 | 国产一区二区欧美 | 国产午夜亚洲精品 | xxxx8| 亚洲人成综合第一网 | 麻豆国产网站 | 中文字幕在线免费播放 | av在线播放亚洲 | 一级做a爱片久久毛片a高清 | 999精品久久久 | 精品一区二区6 | 国产手机av在线 | 99久久婷婷国产综合精品青牛牛 | 免费一级毛片免费播放 | 国产在线观看免费视频软件 | 黄色的视频免费观看 | 国产伊人色 | 精品国产91久久久久久 | 亚洲午夜一区二区三区 | 久草干| 国产污污视频 | 黄色av免费| 国产精品视频自拍 | 欧美成人黄色 | 黄色毛片视频在线观看 | 久久精品视频16 | 午夜生活理论片 | 国产精品自拍片 | 色中色激情影院 | jizzjizz中国少妇中文 |