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.

解法:

  这道题让我们移除一个数组中和给定值相同的数字,并返回新的数组的长度。是一道比较容易的题,我们只需要一个变量用来计数,然后遍历原数组,如果当前的值和给定值不同,我们就把当前值覆盖计数变量的位置,并将计数变量加1。代码如下:

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

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

    Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

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

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

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

  10. 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. 按照Right-BICEP要求设计的测试用例

    测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...

  2. 给个理由走下去——读《我是一只IT小小鸟》有感

    和很多人一样,高考失利,迷迷茫茫的走进了软件学院.关于这个专业,具体学什么是一概不知,只知道学软件的很帅很帅,幻想着以后当个行侠仗义的黑客,或是开发一款自己的游戏都是十分诱惑人的.然而这个世界有个不成 ...

  3. 【Nginx】配置说明

    #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug ...

  4. javascript之容易出错的地方

    1: 不是所有的非空对象都有toString()方法的 var obj = Object.create(null); console.log(obj.toString());   // false; ...

  5. struct和class的联系与区别

    C++支持另一个关键字struct,它也可以定义类类型.struct关键字是从C语言继承过来的. 默认初始访问级别不同: 如果使用class关键字来定义类,那么定义在第一个访问标号前的任何成员都隐式指 ...

  6. 第82天:jQuery中prop()和attr()的区别

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...

  7. AngularJS中$apply

    $apply是$scope下的特性,传播model的变化.下面的例子两秒之后控制台会显示出已经更新的model, 然而, view 并没有更新.$digest循环不会只运行一次.在当前的一次循环结束后 ...

  8. 题解 P1567 【统计天数】

    天哪!竟然没人用优先队列! 小金羊又来水题了... 优先队列中的大根堆帮助我们把时间复杂度降到O(n+logn) 首先我们先了解一下大根堆... 大根堆的性质:根节点比子节点大, 这意味着最上面的节点 ...

  9. [洛谷P1642]规划

    题目大意:有一棵$n(n\leqslant100)$个点的树,每个点有两个权值$a,b$,要求选择一个$m$个点的连通块$S$,最大化$\dfrac{\sum\limits_{i\in S}a_i}{ ...

  10. Redis存储系统

    一.QuickStart 1.Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string ...