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.

对排好序的数列去重。返回不重复的数的个数n,并将其一次排列在原来数组的前n位。

代码如下:

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())
{
return ;
}
int n = nums.size();
if(n == )
{
return ;
}
int sum = ;
int j = ;
for(int i = ; i < n; i++)
{
if(nums[j] != nums[i])
{
nums[j+] = nums[i];
sum++;
j++;
} }
return sum;
}
};

leetcode 26的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  2. 前端与算法 leetcode 26. 删除排序数组中的重复项

    目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...

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

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  4. Java实现 LeetCode 26 删除排序数组中的重复项

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

  5. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

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

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

  7. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  8. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  9. [LeetCode]26. 删除排序数组中的重复项(数组,双指针)

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

随机推荐

  1. 1小时vpn coding让开发更简单 或https://www.imfreevpn.org/

  2. spring controller中@Value取不到applicationContext.xml中加载配置文件的问题

    原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...

  3. ubuntu13.10下安装samba

    1.现在的ubuntu系统都默认安装了samba服务,如果是精简系统的话,可以通过apt-get进行安装 sudo apt-get install samba sudo apt-get cifs-ut ...

  4. 织梦CMS站点favicon.ico图标的放置

    1.在线制作一个ico图标,推荐制作网站:http://ico.55.la/.制作好后,将favicon.ico图标放在站点模板默认目录下的images文件夹里. 2.在index.htm的<h ...

  5. SQL备份表及相关笔记

    create table history1301( remark nvarchar(64))create table history1302( remark nvarchar(64))create t ...

  6. Hdfs增量导入小文件合并的思路

    1.使用mr进行合并 2.使用getmerge 将文件拉取到本地,再上传到hdfs,注意nl参数 3.使用appendToFile 4.使用hadoop提供的打包压缩技术 Usage: hadoop ...

  7. 利用Scanner键盘输入

    Scanner不在java.lang.*包中,故必须import包java.util.Scanner.输入流(InputStream)为System.in 方法如下:Scanner s= new Sc ...

  8. Android——开发环境

    sdk manager——>Tools 开发的工具类 sdk manager——>Extras——>Android support Library 支持高版本应用向低版本兼容 sdk ...

  9. access的查询中具体到时间的时候使用“#”

    如果不使用#的情况下会导致出现操作符丢失的错误提示,因此再比较时间的时候将日期和时间用#包括起来: 例如: SELECT *FROM ms_record where dateTo > #2015 ...

  10. 移植samba到ios平台,最新支持ios8.3

    移植samba到ios平台,最新支持ios8.3 下载https://github.com/kolyvan/kxsmb 开源项目 1  cd samba-4.1.14 2  vi Rakefile 修 ...