给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度。
不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点。
示例:
给定数组: nums = [1,1,2],
你的函数应该返回新长度 2, 并且原数组nums的前两个元素必须是1和2
不需要理会新的数组长度后面的元素
详见:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

Java实现:

class Solution {
public int removeDuplicates(int[] nums) {
int size=nums.length;
if(size==0||nums==null){
return 0;
}
int index=1;
for(int i=1;i<size;++i){
if(nums[i]!=nums[i-1]){
nums[index]=nums[i];
++index;
}
}
return index;
}
}

C++实现:

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
int index=1;
for(int i=1;i<size;++i)
{
if(nums[i]!=nums[i-1])
{
nums[index]=nums[i];
++index;
}
}
return index;
}
};

026 Remove Duplicates from Sorted Array 从排序数组中删除重复项的更多相关文章

  1. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  2. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  6. arts打卡 从排序数组中删除重复项

    Algorithm 从排序数组中删除重复项     给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组 ...

  7. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  8. [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  9. 【LeetCode】从排序数组中删除重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...

随机推荐

  1. Python:extend和append的用法

    转于:https://www.cnblogs.com/subic/p/6553187.html 博主:subic 1)list.append(object) 向列表中添加一个对象object2)lis ...

  2. 【转】 Pro Android学习笔记(六二):Preferences(6):header

    目录(?)[-] 代码实现 header xml文件 在前面的例子,我们主要学习了PreferenceScreen的xml如何写,preference有哪些类型.在代码中,我们为了不提示warning ...

  3. PHPstorm 常用快捷键操作

    1.ctrl+ N: 查找类 2.ctrl+ shift+ N: 全局搜索文件 ,优先文件名匹配的文件 3.ctrl + G: 定位行,跳转行 4.ctrl + F12: 显示当前页面类的所有方法 / ...

  4. [原创] 新人分享--ORA-01012:not logged on的解决办法 [复制链接]

    转自:http://f.dataguru.cn/thread-82530-1-1.html

  5. LAMP 1.2 Apache编译安装问题解决

    这个错误安装 yum install -y gcc error: mod_deflate has been requested but can not be built due to prerequi ...

  6. docker里安装ubuntu

    使用 Ubuntu 官方镜像 Ubuntu 相关的镜像有很多,这里使用 -s 10 参数,只搜索那些被收藏 10 次以上的镜像 $ docker search -s 10 ubuntu NAME DE ...

  7. Hive 查询优化总结

    一.join优化 Join查找操作的基本原则:应该将条目少的表/子查询放在 Join 操作符的左边.原因是在 Join 操作的 Reduce 阶段,位于 Join 操作符左边的表的内容会被加载进内存, ...

  8. 5.docker的疑难杂症

    根据官方文档:https://docs.docker.com/install/linux/docker-ce/centos/搭建docker 1.卸载docker旧版本: sudo yum remov ...

  9. 手写一个admin 组件------STARK

    开一个新的项目,,建立一个stark 包, 在里面创建一个service包,在service 包里创建一个stark.py 文件, 配置好环境, makemigreations, migreate. ...

  10. 计算像素亮度(Pixel Light)

    RGB有亮度吗?常用公式: Y(亮度)=(0.299*R)+(0.587*G)+(0.114*B)