Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ remove elements

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how C++ removes elements". In daily operation, I believe many people have doubts about how C++ removes elements. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to remove elements by C++"! Next, please follow the editor to study!

Remove Element 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.

Example 1:

Given nums = [3Jing 2 Jing 2 Jue 3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

It doesn "t matter what you leave beyond the returned length.

Example 2:

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

Your function should return length =

five

, with the first five elements of

Nums

Containing

0

one

three

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.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

/ / nums is passed in by reference. (I.E., without making a copy)

Int len = removeElement (nums, val)

/ / any modification to nums in your function would be known by the caller.

/ / using the length returned by your function, it prints the first len elements.

For (int I = 0; I < len; iTunes +) {

Print (nums [I])

}

This problem allows us to remove a number from an array that is the same as the given value and return the length of the new array. Is an easy problem, only one variable is needed to count, and then traverse the original array, if the current value is different from the given value, the current value overwrites the position of the counting variable and adds 1 to the counting variable. The code is as follows:

Class Solution {public: int removeElement (vector& nums, int val) {int res = 0; for (int I = 0; I < nums.size (); + + I) {if (nums [I]! = val) nums [res++] = nums [I];} return res;}}; at this point, the study on "how C++ removes elements" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report