Leetcode 27. Remove Element (Easy) (cpp)
2016-07-12 11:35
961 查看
Leetcode 27. Remove Element (Easy) (cpp)
Tag: Array, Two Pointers
Difficulty: Medium
Tag: Array, Two Pointers
Difficulty: Medium
/* 27. Remove Element (Easy) 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. */ class Solution { public: int removeElement(vector<int>& nums, int val) { int index = 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] != val) nums[index++] = nums[i]; } return index; } }; class Solution { public: int removeElement(vector<int>& nums, int val) { return distance(nums.begin(), remove(nums.begin(), nums.end(), val)); } };
相关文章推荐
- C++11多线程系列第一部分
- C++ split 分割字符串
- C++ #pragma 用法小结
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (cpp)
- JNI使用实例之C、C++ DLL回调java成员函数
- 串算法 - 蛮力匹配 Brute Force
- 《C++ Primer Plus》12.7 队列模拟 学习笔记
- C语言入门(十二)函数的定义和调用
- C++函数模板深入理解1
- Leetcode 3. Longest Substring Without Repeating Characters (Medium) (cpp)
- c++继承
- C语言:自增和自减
- Leetcode 2. Add Two Numbers (Medium) (cpp)
- C语言入门(十一)多维数组
- C++ Copy Elision
- Leetcode 1. Two Sum (Easy) (cpp)
- C#加载C、C++动态库的方法
- C语言入门(十)数组
- C++中继承和调用父类的构造函数方法,外加MFC常见dialog继承
- 巩固基础篇:算法复杂度的分析方法及其运用