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 to delete duplicates in a sorted array in LeetCode

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to delete duplicates in the sorted array in LeetCode. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

First, delete the duplicate item II1 in the sorted array. Brief description of the problem.

Given an ascending array nums, you need to delete the repeating elements in place so that each element appears at most twice, and returns the new length of the removed array.

Do not use extra array space, you must modify the input array in place and do it using O (1) extra space.

Description:

Why is the returned value an integer, but the output answer is an array?

Note that the input array is passed as a reference, which means that modifying the input array in the function is visible to the caller.

You can imagine the internal operation as follows:

/ / nums is passed as a "reference". That is, no copy of the argument is made.

Int len = removeDuplicates (nums)

/ / modifying the input array in the function is visible to the caller.

/ / according to the length returned by your function, it will print out all the elements in the array within that length range.

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

Print (nums [I])

}

2, example description example 1:

Input: nums = [1, 1, 1, 1, 2, 2, 3]

Output: 5, nums = [1, 1, 1, 2, 2, 3]

Explanation: the function should return the new length length = 5, and the first five elements of the original array are modified to 1, 1, 2, 2, 3. You don't need to consider the elements in the array that exceed the new length.

Example 2:

Input: nums = [0re0pence1jing1jing1jing1jingjinmei 2jinme3jin3]

Output: 7, nums = [0pl 0rem 1m 1m 2m 3e 3]

Explanation: the function should return the new length length = 7, and the first five elements of the original array are modified to 0,0,1,1,2,3,3. You don't need to consider the elements in the array that exceed the new length.

Tip:

0

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