Leetcode - 4Sum
Leetcode - Range Sum Query - Mutable

Leetcode - Remove Element

violet posted @ Mar 29, 2020 08:08:04 AM in 算法 with tags Algorithm Golang TwoPointers , 285 阅读

https://leetcode.com/problems/remove-element/

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

 

func removeElement(nums []int, val int) int {
    fast := 0
    slow := 0
    for slow < len(nums) && nums[slow] != val {
        slow++
    }
    fast = slow+1
    for slow < len(nums) && fast < len(nums) {
        for fast < len(nums) && nums[fast] == val {
            fast++
        }
        if fast > len(nums)-1 {
            break
        }
        nums[slow] = nums[fast]
        slow++
        fast++
    }
    return slow
}
meilleur android aut 说:
Jul 17, 2023 10:55:01 PM

Android Auto est une méthode courante pour connecter votre appareil Android au système d’infodivertissement de votre voiture informations diffusées et accéder à des fonctions telles que la navigation, la musique et la messagerie. meilleur android auto sans fil Alors que de nombreux modèles de voitures plus récents incluent la prise en charge intégrée d’Android Auto, les véhicules plus anciens peuvent ne pas l’être. Les adaptateurs sans fil Android Auto peuvent vous aider.

NCERT science Exemp 说:
Sep 27, 2023 04:12:08 PM

CBSE 8th Science Exemplar Problems with Solutions 2024 was introduced by the Central Board of Secondary Education (CBSE) to develop the Problem Solving skills and higher order Thinking Skills of Students, Detailed and Appropriate Solutions NCERT science Exemplar Solutions for 8th 2024 are helpful Students in Solving Questions in the Competitive Exam CBSE 8th Class Science Books 2024 are Quite Important when we talk about the CBSE Exam as these 8th Science Exemplar Problems and Solutions are Prepared as per the Syllabus Prescribed for Central Board Exam, so the CBSE 8th Science Exemplar Problems and Solution 2024 can be Considered as the Complete Preparation Material for the Exam.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter