[Leetcode]-Remove Duplicates from Sorted Array
LeetCode from Array remove sorted Duplicates
2023-09-11 14:20:59 时间
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
Hide Tags :Array 。Two Pointers
题目26:输入一个已经拍好序的数组。当中有可能有反复数据。找出反复数据。栓出多余的反复数据。仅仅保留一个反复数据。
思路:记录当中的反复数据次数。比方当中有三个数据都是5,那么反复次数就为2。
将后面的数据依次填充到反复数据位置.
if(nums[i] == nums[i+1]) count++;
else nums[i+1-count] = nums[i+1];
题目27:本题和题26相似。27题要求将与给定数据val反复数据都栓出,不保留不论什么一个反复数据,方法相似,记录反复次数比26题多1个即可
if(nums[i] == val) count++;
else nums[i-count] = nums[i];
#include <stdlib.h>
#include <stdio.h>
//26:Remove Duplicates from Sorted Array
/* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
* Do not allocate extra space for another array, you must do this in place with constant memory.
* For example,
* Given input array nums = [1,1,2],
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
*/
//思路一直累加反复的个数,数组第i 位= i - 反复的个数
int removeDuplicates(int* nums, int numsSize) {
int i = 0;
int count = 0;
for(i=0;i<numsSize-1;i++)
{
if(nums[i] == nums[i+1])
{
count++;
}
else
{
nums[i+1-count] = nums[i+1];
}
}
return numsSize-count;
}
//27 :Remove Element
//Given an array and a value, remove all instances of that value in place and return the new length.
//The order of elements can be changed. It doesn't matter what you leave beyond the new length.
int removeElement(int* nums, int numsSize, int val) {
int i = 0;
int count = 0;
for(i=0;i<numsSize;i++)
{
if(nums[i] == val)
{
count++;
}
else
{
nums[i-count] = nums[i];
}
}
return numsSize-count;
}
int main()
{
int n[10] = {1,2,3,3,3,4,5,5,6,7};
int s[10] = {1,2,3,4,5,6,7,8,9,10};
int r = removeDuplicates(s,10);
printf("r = %d\n",r);
int i = 0 ;
for(i=0;i<r;i++)
{
printf("n[%d] = %d\n",i,n[i]);
}
}
相关文章
- Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode: Remove Nth Node From End of List
- Leetcode: Remove Duplicates from Sorted List
- LeetCode——Remove Duplicates from Sorted List II
- LeetCode Remove Duplicates from Sorted Array II
- 【Leetcode】169. 多数元素(简单)
- JS leetcode 拥有最多糖果的孩子 题解分析,六一快乐。
- [算法]LeetCode 152:乘积最大子序列
- [LeetCode] Remove Duplicates from Sorted List II
- [LeetCode] Length of Last Word
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode刷题
- 【算法/动态规划】leetcode刷题路线(持续更新)
- 【LeetCode】26. Remove Duplicates from Sorted Array
- [leetcode]Two Sum
- leetcode Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 1298. Maximum Candies You Can Get from Boxes 你能从盒子里获得的最大糖果数
- [LeetCode] 1162. As Far from Land as Possible 地图分析
- [LeetCode] 943. Find the Shortest Superstring 找到最短的超级字符串
- [LeetCode] 1028. Recover a Tree From Preorder Traversal 从先序遍历还原二叉树
- [LeetCode] Asteroid Collision 行星碰撞
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
- [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值
- [LeetCode] Power of Four 判断4的次方数
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二