Original title address: https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/21/
Title Description:
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 array after removal.
Instead of using extra array space, you must modify the input array in place and complete it with O (1) Extra space.
Example 1:
Nums 1
2
. You do not need to consider elements that are beyond the new length in the array.
Example 2:
Given 0
1
2
3
4
. You do not need to consider elements that are beyond the new length in the array.
Description
Why is the return value an integer, but the answer to the output is an array?
Note that the input array is passed in a "reference" manner, which means that modifying the input array in the function is visible to the caller.
You can imagine the internal operation as follows:
// The nums is passed in a "reference" manner. In other words, do not make any copy of the argument int len = removeduplicates (nums); // modifying an input array in a function is visible to the caller. // depending on the length returned by your function, it prints all elements within that length range in the array. for (int0; i < Len; i++) { print (nums[i]);}
Java soultion code is as follows:
classSolution { Public intRemoveDuplicates (int[] nums) { if(nums==NULL|| Nums.length==0){ return0; } intCur=0; for(intpre=0;pre<nums.length;pre++){ if(nums[cur]!=Nums[pre]) {nums[++cur]=Nums[pre]; } } return++cur; }}
Problem Solving Ideas:
We use fast and slow pointers to record the coordinates of the traverse, and at the very beginning both pointers point to the first number,
If the two pointers refer to the same number, the fast pointer goes one step forward, and if different, the two pointers move forward, so that when the fast pointer finishes the entire array, the current coordinates of the slow pointer and 1 are the number of different numbers in the array.
Let's look at the test code below:
Public classRemovedatafromsortedarraytest {/*** 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 array after removal. * Do not use extra array space, you must modify the input array in situ and complete with O (1) Extra space. */ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//int[] Nums = {1, 1, 2}; int[] Nums = {0,0,1,1,1,2,2,3,3,4}; for(inti=0;i<nums.length;i++) {System.out.println ("nums[" +i+ "]=" +Nums[i]); } System.out.println ("--------------------"); //The nums is passed in a "reference" manner. That is, no copies of the arguments are made intLen =removeduplicates (nums); //modifying an input array in a function is visible to the caller. //depending on the length returned by your function, it prints all the elements within that length range in the array. for(inti = 0; i < Len; i++) {System.out.println ("nums[" +i+ "]=" +Nums[i]); } } Public Static intRemoveDuplicates (int[] nums) { if(nums==NULL|| Nums.length==0) { return0; } intCur=0; for(intpre=0; Pre < Nums.length; pre++) { //move the slow pointer when the number is not at the same time if(nums[cur]!=Nums[pre]) {nums[++cur]=nums[pre];//equivalent to cur=cur+1; Nums[cur]=nums[pre]; } } return++cur;//equivalent to cur+1 }}
Attention:
The problem has to be made clear here is that the array passed by the function call is a reference pass instead of a value pass.
Since arrays can be directly used with cursors pointers, the two fast and slow pointers we define at the beginning point to the same address
The first loop executes and this, because the slow pointer cur and the fast pointer pre point to the same number, so the full pointer does not change.
Continue the loop, the slow pointer cur and the fast pointer pre data are different, when we slow the pointer to start moving
Execute this statement
Nums[++cur]=nums[pre];
At this point the cur points to the beginning of the change, the execution content is like
Cur[0]=0, cur[1]=pre[2];
And so on, we can easily calculate the desired results.
We recall the concept of arrays, the so-called arrays, which are ordered sequence of elements
Because the arrays are ordered, duplicate elements must be contiguous and no additional records are required. Time complexity is O (n), Spatial complexity O (1).
1. Problem solving report ~ Remove duplicates from the sorted array