[Algorithm-Elementary-Array] Delete duplicates in sorted array (multi-language version implementation)
🌶 Blog Description and Acknowledgments
🎆🎆🎆 Part of the information involved in the article is compiled from the Internet, which contains my own personal summary and opinions. The purpose of sharing is to build a community and consolidate myself.
💗💗💗 If the cited information is infringing, please contact me to delete it!
❤️🔥❤️🔥❤️🔥 Thanks almighty network!
🥪🥪🥪 and hardworking own , personal blog , GitHub study ,
🍿🍿🍿 Public [Gui Zimo] , Mini Program [Zimo said]
👍👍👍 If you feel that it is helpful to you, please give me a thumbs up and encouragement. Remember to bookmark the good text! Follow me to grow together!
💐💐💐 Fortunately I am here, thank you for coming!
🍄 Algorithm Description
Language is only a means to implement algorithms, ideas are the most important.
If there are multiple solutions, choose only one language as the solution comparison.
If a certain algorithm is implemented alone, it will be implemented in multiple languages, and the characteristics of the languages will be compared.
😎Because if it is many-to-many, the space will be larger, which will affect the viewing experience!
🍖 topic
address
26. Remove duplicates in an ordered array
Given an ordered array nums, please delete the repeated elements in place so that each element appears only once, and return the new length of the deleted array.
Topic description
Do not use extra space for an array, you must modify the input array in place and in use under conditions of O (1) extra space to complete .
instruction
Why is the returned value an integer, but the output answer is an array?
Note that the input array is passed "by reference", which means that changes to the input array within the function are visible to the caller.
You can imagine the internal operation as follows:
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Example 1
输入:nums = [1,1,2]
输出:2, nums = [1,2]
解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
Example 2
输入:nums = [0,0,1,1,1,2,2,3,3,4]
输出:5, nums = [0,1,2,3,4]
解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
hint
0 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums 已按升序排列
🥕 Ideas
violent thoughts
(Note that it is a violent idea, not a violent solution!)
As a straight man, I just want to achieve it.
Direct traversal, see that the title has been determined to be in order, and if it is not equal to the previous one, directly get it and store it in a new array. After traversing the direct new array is the answer.
It looks like it's very close! It's a simple subject after all.
but and under the condition of O(1) extra space. This is a hurdle that we cannot overcome. In this case, you have to consider operating on the original array.
Operate on the original array, first on the double pointer!
double pointer
ideas
The fast and slow pointers play, the fast pointer is fast
, and the slow pointer is low
.
The array is ordered, so repeated elements must be adjacent. Operate in the same array, that is, move the elements that are not repeated to the left of the array, and finally take the value of the array on the left.
Algorithm flow
Compares the elements at positions
fast
andlow
Loop execution:
- If equal,
fast
shifted by 1 bit - If not equal, the
low
value before a changedfast
,low
after a shift,fast
backward 1
- If equal,
End of loop:
fast
bounds
- End of loop, return new array length
low + 1
diagram
This will be the most soulful moment. Algorithms without diagrams are hooligans! (Hahaha, I will try to correct my previous hooliganism!)
In fact, it took me a lot of time to draw pictures, but I don't think it's a loss, I remember it more deeply!
Let's make a GIF! (Wrote a small tool to generate GIF from pictures in Python)
JavaScript
There is nothing special to say. If I force it, I will take the length calculation of nums
code
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
// 判断边界
if (nums && nums.length < 0) {
return 0
}
var low = 0, fast = 1, n = nums.length;
while (fast < n) {
if (nums[fast] !== nums[low]) {
nums[low + 1] = nums[fast]
low++
}
fast++
}
return low + 1
};
Java
Code
class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length < 1) {
return 0;
}
int fast = 1, low = 0, n = nums.length;
while(fast < n) {
if (nums[fast] != nums[low]){
nums[low + 1] = nums[fast];
low++;
}
fast++;
}
return low + 1;
}
}
Python3
Code
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
fast = 1
low = 0
while fast < n:
if nums[fast] != nums[low]:
nums[low + 1] = nums[fast]
low += 1
fast += 1
return low + 1
PHP
Code
class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
function removeDuplicates(&$nums) {
if ($nums == null || count($nums) < 0) {
return 0;
}
$fast = 1;
$low = 0;
$n = count($nums);
while ($fast < $n) {
if ($nums[$fast] != $nums[$low]) {
$nums[$low + 1] = $nums[$fast];
$low++;
}
$fast++;
}
return $low + 1;
}
}
Go
Note that the go language has no while
code
func removeDuplicates(nums []int) int {
n := len(nums)
if n < 1 {
return 0
}
low := 0
for fast := 1; fast < n; fast++ {
if nums[fast] != nums[low] {
nums[low + 1] = nums[fast]
low++
}
}
return low + 1
}
C++
code
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if (n < 1) {
return 0;
}
int fast = 1, low = 0;
while (fast < n) {
if (nums[fast] != nums[low]) {
nums[low + 1] = nums[fast];
low++;
}
fast++;
}
return low + 1;
}
};
C
code
int removeDuplicates(int* nums, int numsSize){
if(numsSize == 0) {
return 0;
}
int fast = 1, low = 0;
while (fast < numsSize) {
if (nums[fast] != nums[low]) {
nums[low + 1] = nums[fast];
low++;
}
fast++;
}
return low + 1;
}
🥦 Summary
Start with arrays, think about how to extract ideas 🤔 array extraction, disassemble step by step from simple to complex, and also improve the data usage skills of programming languages.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。