Leetcode Remove Element

Source: Internet
Author: User

The original problem of remove element of leetcode problem solving

Deletes an element of a particular value in an array, returning the deleted array length.

Note the point:

    • The order of numbers after the operation does not need to be the same as before
    • The part that exceeds the return length does not need to be processed

Example:

Input: Nums [1, 2, 3, 4, 3, 2, 1],val = 1
Output: 5

Thinking of solving problems

Around two pointers move toward the middle, the left pointer finds a value equal to Val, the right pointer finds the first value not equal to Val, and assigns the value to the left pointer to the right pointer. Keep moving toward the middle.

AC Source
 class solution(object):     def removeelement(self, nums, Val):        "" : Type Nums:list[int]: type Val:int:rtype:int "" "left =0right = Len (nums)-1         whileLeft <= right: whileLeft <= Right andNums[left]! = val:left + =1             whileLeft <= Right andNums[right] = = val:right-=1            ifLeft < Right:nums[left] = Nums[right] Left + =1Right-=1        returnRight +1if__name__ = ="__main__":assertSolution (). Removeelement ([1,2,3,4,3,2,1],1) ==5    assertSolution (). Removeelement ([2],3) ==1

Welcome to my GitHub to get the relevant source code.

Leetcode Remove Element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.