zoukankan      html  css  js  c++  java
  • LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显。简单说本题就是让你把有序数组中的重复项给换成正常有序的。比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等。拿起键盘干就行了。然后返回有序项的下标就可以。

    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.

    给定一个排序的数组,删除重复的位置,使每个元素只显示一次并返回新的长度。

    不要为另一个数组分配额外的空间,您必须使用常量内存来执行此操作。

    例如,
    给定输入数组nums = [1,1,2],

    你的函数应该返回length = 2,num的前两个元素分别为1和2。 无论你离开新的长度什么都不重要。

     1 class Solution {
     2     public int removeDuplicates(int[] nums) {
     3          int count=1;
     4             for(int i=1;i<nums.length;i++){
     5                 if(nums[i]!=nums[i-1]){
     6                     nums[count]=nums[i]; //将多余重复项换成正常有序项
     7                     count++;
     8                 }
     9             }
    10             return count;
    11     }
    12 }

  • 相关阅读:
    安卓证书获取sha1的方法 实测有效 原创!!!
    KafKa_原理及项目整合
    微服务-小结
    bd——kafka
    Zookeeper应用——
    idea使用小结
    服务管理框架——Zookeeper
    中间件汇总——了解
    新篇章:大数据——Flume
    70-71中期10道基石
  • 原文地址:https://www.cnblogs.com/xpang0/p/7484794.html
Copyright © 2011-2022 走看看