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 ​ LeetCode removes duplicates in sorted arrays

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

Share

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

This article mainly introduces LeetCode how to delete the duplicates in the sorted array, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

Meaning of the title

Given a sorted array, you need to delete the repeating elements in place so that each element appears only once, returning 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.

Sample

Example 1: the function should return the new length 2 given the array nums = [1Magne 1Mague 2], and the first two elements of the original array nums have been modified to 1Jing 2. You don't need to consider the elements in the array that exceed the new length.

Example 2: given nums = [0rect 0rect 1jiggle 1jiggle 2je 2rec 3je 3je 4], the function should return the new length 5, and the first five elements of the original array nums are modified to 0meme 1,2jol 3,4. You don't need to consider the elements in the array that exceed the new length.

After the problem-solving array is sorted, we can place two pointers I and j, where I is the slow pointer and j is the fast pointer. As long as nums [I] = nums [j], we increase j to skip duplicates. When we encounter nums [j] ≠ nums [I], the operation of skipping duplicates is over, so we must copy the value of its (nums [j]) to nums [I + 1]. Then increment I, and then we will repeat the same process again until j reaches the end of the array. Time complexity: O (n), assuming that the length of the array is n, then I and j traverse n steps at most. Space complexity: O (1).

Public int removeDuplicates (int [] nums) {if (nums.length = = 0) return 0; int I = 0; for (int j = 1; j < nums.length; jacks +) {if (nums [j]! = nums [I]) {icalendar; nums [I] = nums [j];}} return I + 1;}

Thank you for reading this article carefully. I hope the article "how to delete duplicates in the sorted array by LeetCode" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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