Comments (8)
Just used two pointers referring left and right to resolve this puzzle, but it seems that brute force approach is more complicated than the two pointers approach. Actually the two pointers approach showcased is O(2n) in time complexity since fastIndex and slowIndex will step n+n steps in the worst case.
from leetcode-master-comment.
打卡第一天: 很喜欢快慢指针这样的逻辑. 快指针速度一定快于慢指针, 在快指针扫描的过程中, 将所有不等于 target 的值, 都一个一个的复制到慢指针对应的内存中. 最终结束的时候, 慢指针指向的就是目标数组的最后一个元素的后一位. 刚开始看的时候有点疑惑, 等想明白了就豁然开朗了. 代码也变得相当的精简.
from leetcode-master-comment.
打卡
from leetcode-master-comment.
打卡
from leetcode-master-comment.
借鉴双指针算法,也可以用滑动窗口:
func removeElement(nums []int, val int) int {
sort.Ints(nums)
left, right := 0, len(nums)-1
for left <= right {
if nums[left] == val {
nums[left], nums[right] = nums[right], nums[left]
right--
} else {
left++
}
}
return left
}
from leetcode-master-comment.
打卡
from leetcode-master-comment.
打卡day2
from leetcode-master-comment.
对于C++语言,vector容器提供了erase成员函数删除指定位置的数组元素,或指定左闭右开区间的部分数组元素,本题使用前者。(当然其他语言未必有类似的函数,故双指针或暴力解题是更普适的方法)
class Solution {
public:
int removeElement(vector& nums, int val) {
int res = 0;
for (int i = 0; i < nums.size(); ) {
if (nums[i] == val) {
// 如果本元是 val则擦除,擦除后指针位置不用变!因为擦除后就自动指下一个元了
nums.erase(nums.begin() + i);
} else {
// 如果当前元素不是 val,则手动移动到下一个位置,并且res++
i++;
res++;
}
}
return res;
}
};
from leetcode-master-comment.
Related Issues (20)
- [Vssue]0055.跳跃游戏.md
- [Vssue]0045.跳跃游戏II.md
- [Vssue]1005.K次取反后最大化的数组和.md
- [Vssue]周总结/20201203贪心周末总结.md
- [Vssue]0134.加油站.md
- [Vssue]0135.分发糖果.md
- [Vssue]0860.柠檬水找零.md
- [Vssue]0406.根据身高重建队列.md
- [Vssue]周总结/20201217贪心周末总结.md
- [Vssue]根据身高重建队列(vector原理讲解).md
- [Vssue]0452.用最少数量的箭引爆气球.md
- [Vssue]0435.无重叠区间.md
- [Vssue]0763.划分字母区间.md
- [Vssue]0056.合并区间.md
- [Vssue]周总结/20201224贪心周末总结.md
- [Vssue]0738.单调递增的数字.md
- [Vssue]0968.监控二叉树.md
- [Vssue]贪心算法总结篇.md
- [Vssue]xunlian/xunlianying.md
- [Vssue]kamacoder/图论并查集理论基础.md
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode-master-comment.