In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to delete duplicates in the sorted array in LeetCode. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
First, delete the duplicates in the sorted array
1. Brief introduction of the problem.
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.
2, example description example 1:
Given array nums = [1, 1, 1, 2]
The function should return the new length 2, and the first two elements of the original array nums are modified to 1, 2.
You don't need to consider the elements in the array that exceed the new length.
Example 2:
Given nums = [0re0jing1jing1jing1jing2jing2jinme3jpj4]
The function should return the new length 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4.
You don't need to consider the elements in the array that exceed the new length.
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])
}
3, the way to solve the problem
Two ways of thinking: (1) for the first time, when traversing the array, let the first element act as a "sentinel" node, and then traverse the data to compare the following elements, but remember to update the data of the "sentry" node every time you traverse the array; (2) operate on the collection according to the hashMap key
4, problem solving procedure
Import java.util.HashMap
Import java.util.Map
Public class RemoveDuplicatesTest2 {
Public static void main (String [] args) {
Int [] nums = {1,1,2}
Int removeDuplicates = removeDuplicates2 (nums)
System.out.println ("removeDuplicates =" + removeDuplicates)
}
Public static int removeDuplicates (int [] nums) {
Int len = nums.length
If (0 = = len) {
Return 0
}
Int prev = nums [0]
Int newLen = 1
For (int I = 1; I < len; iTunes +) {
Int num = nums [I]
If (num! = prev) {
Nums [newLen++] = num
Prev = num
}
}
Return newLen
}
Public static int removeDuplicates2 (int [] nums) {
If (nums = = null | | nums.length = = 0) {
Return 0
}
Map map = new HashMap (nums.length)
Int index = 0
For (int I = 0; I < nums.length; iTunes +) {
Int value = map.getOrDefault (nums [I], 0) + 1
Map.put (nums [I], value)
If (value
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.