leecode 解题总结:27 Remove Element
2017-02-03 17:35
375 查看
#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 = 3
Your 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 1
3 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;
}
#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 = 3
Your 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 1
3 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;
}
相关文章推荐
- leecode 解题总结:47. Permutations II
- leecode 解题总结:55. Jump Game
- leecode 解题总结:29 Divide Two Integers
- leecode 解题总结:38 Count and Say
- leecode 解题总结:35. Search Insert Position
- leecode 解题总结:48. Rotate Image
- leecode 解题总结:42. Trapping Rain Water
- leecode 解题总结:26 Remove Duplicates from Sorted Array
- leecode 解题总结:25 Reverse Nodes in k-Group
- leecode 解题总结:40 Combination Sum II
- leecode 解题总结:51. N-Queens
- leecode 解题总结:17. Letter Combinations of a Phone Number
- leecode 解题总结:16 3Sum Closest
- leecode 解题总结:28 Implement strStr()
- leecode 解题总结:21. Merge Two Sorted Lists
- leecode 解题总结:53. Maximum Subarray
- leecode 解题总结:15 3Sum
- leecode 解题总结:19 Remove Nth Node From End of List
- leecode 解题总结:32 Longest Valid Parentheses
- leecode 解题总结:41. First Missing Positive