Git Product home page Git Product logo

Comments (8)

WEITAOLEE avatar WEITAOLEE commented on May 30, 2024 2

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.

fengbaobao avatar fengbaobao commented on May 30, 2024

打卡第一天: 很喜欢快慢指针这样的逻辑. 快指针速度一定快于慢指针, 在快指针扫描的过程中, 将所有不等于 target 的值, 都一个一个的复制到慢指针对应的内存中. 最终结束的时候, 慢指针指向的就是目标数组的最后一个元素的后一位. 刚开始看的时候有点疑惑, 等想明白了就豁然开朗了. 代码也变得相当的精简.

from leetcode-master-comment.

mbnabc avatar mbnabc commented on May 30, 2024

打卡

from leetcode-master-comment.

XuChenn avatar XuChenn commented on May 30, 2024

打卡

from leetcode-master-comment.

blkcor avatar blkcor commented on May 30, 2024

借鉴双指针算法,也可以用滑动窗口:
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.

aptyyds avatar aptyyds commented on May 30, 2024

打卡

from leetcode-master-comment.

dkp339 avatar dkp339 commented on May 30, 2024

打卡day2

from leetcode-master-comment.

superGinga avatar superGinga commented on May 30, 2024

对于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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.