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. [转]iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息.  命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devi ...

  2. Android Studio22-NDK-LLDB调试

    Android Studio2.2更好的支持NDK开发,并可以像开发java一样的DEBUG程序,不需要添加gradle-experimental插件,就可调试代码! 一,下载 NDK 和构建工具 要 ...

  3. Asp.Net MVC<九>:OWIN,关于StartUp.cs

    https://msdn.microsoft.com/zh-cn/magazine/dn451439.aspx(Katana 项目入门) 一不小心写了个WEB服务器 快刀斩乱麻之 Katana OWI ...

  4. jquery 实现类似于弹幕效果

    在别人网站中看到一个类似于弹幕的效果,闲来无事用jquery写了个备用~~ <!DOCTYPE html> <meta charset="utf-8"> & ...

  5. Python笔记(1)变量与表达式

    列表list list是用的最多的类型 可以count计数 可嵌套,多钟类型并存 支持 + * a = [1,2,3] a_ref = a a_copy = a[:] 引用,a变化a_ref也变化 指 ...

  6. 什么是js面向对象??

    简单的来说就是键值对,写一个函数,然后传值进去,   function Person(name,age){           this.name = name;           this.age ...

  7. USB Keyboard Recorder

    catalogue . 引言 . Device Class Definition for Human Interface Devices (HID) . USB HID Report Descript ...

  8. JS中匿名函数$(function(){ })和(function(){})()的区别

    “$(function(){ });” Jquery语法的匿名函数,用于存放操作DOM对象的代码,执行其中代码时DOM对象已存在: (通过这样就可以在页面加载完成时通过ajax再异步加载一些数据) “ ...

  9. 【Beta版本】冲刺随笔汇总

    [Beta版本]冲刺计划及安排 [Beta版本]冲刺-Day1 [Beta版本]冲刺-Day2 [Beta版本]冲刺-Day3 [Beta版本]冲刺-Day4 [Beta版本]冲刺-Day5 [Bet ...

  10. <<< ajax在jsp中对于https跨域不能访问

    XMLHttpRequest cannot load https://www.emaple.com. No 'Access-Control-Allow-Origin' header is presen ...