Problem:

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.

Summary:

去除已排序数组中得重复数字,并返回最终数组所含数字个数。

Solution:

用两个指针,指针i遍历数组,指针j标记当前已去重数组的结尾位置,每遍历到一个新数字将其放入j处。

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

LeetCode 26 Remove Duplicates from Sorted Array的更多相关文章

  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. Remove Duplicates from Sorted Array 有序数组中去除重复项

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

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

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

  4. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  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. Java [leetcode 26]Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  7. Leetcode 26. Remove Duplicates from Sorted Array (easy)

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

  8. [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 ...

  9. 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 ...

随机推荐

  1. WCF中的错误及解决办法

    一 .    HTTP 无法注册 URL http://+:8000/Users/.进程不具有此命名空间的访问权限今天按照网上的例子开始学习WCF程序,运行的时候却发现出如下问题:HTTP 无法注册 ...

  2. 一个简单的Linq to TreeNode

    最近两天写单元测试,碰到需要验证一个树是否是期望的,但是树这个结构要验证起来还真是有点烦... 我的树大概是这样的: class TreeNode<T> { ]; public TreeN ...

  3. 在finally中调用一个需要await的方法

    最近在把code改写成async+await的形式,发现有些情况下需要在finally中需要调用异步方法,但是编译器不允许在cache和finally中出现await关键字...但是用Wait()或者 ...

  4. excel出现错误1327 无效的驱动器

    错误描述:错误1327 无效的驱动器 错误程序:excel 2003   这是office安装源的位置被移动造 成的的,大部分时候安装时可能使用了移动硬盘或者在安装后调整了盘符就会造成这个问题.官方的 ...

  5. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 751[Submit][Statu ...

  6. winform刷新UI界面

    this.Invoke(new Action(() => { // 更新使用次数 this.labCount.Text = count; }));

  7. bzoj2064[和谐社会模拟赛]分裂

    题意:给定一个初始集合和目标集合,有两种操作:1.合并集合中的两个元素,新元素为两个元素之和 2.分裂集合中的一个元素,得到的两个新元素之和等于原先的元素.要求用最小步数使初始集合变为目标集合,求最小 ...

  8. backup mysql

    #!/bin/bashcd /home/Licw/backup_openDBNow=$(date +"%m-%d-%Y--%H:%M:%S")#echo $NowFile=$Now ...

  9. 面试题目——《CC150》Java

    package cc150.java; import java.util.Iterator; public class CircularArray { public static void main( ...

  10. Linux下忘记MySQL的root密码的解决方法

    恢复方法之一 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也 ...