In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how LeetCode to remove elements, I hope you have something to gain after reading this article, let's discuss it together!
1
Title Description
Given an array numbers and a value val, remove all elements equal to val in place and return the new length of the removed array. Only O (1) extra space is required, and the order of array elements can be changed. For example, enter [3,2,2,3], 3, and return 2.
2
answer key
Idea: double pointer in turn to determine whether the elements in the list and val equal, equal to eliminate. With LeetCode brush question DAY 23: delete duplicate items in the sorted array similar situation, from left to right to look for attention to the impact of changes in the length of the array, the specific code is as follows, to avoid this impact makes the code simpler can be found from right to left, here temporarily do not show the code. class Solution: def removeElement(self, nums: List[int], val: int) -> int: if len(nums)==0: return i=0 while i < len(nums): if nums[i] == val: #Delete a value somewhere in the list nums.pop(i) else: i+=1 return len(nums)
After reading this article, I believe you have a certain understanding of "LeetCode how to remove elements". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.