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 }

  • 相关阅读:
    声明:此资源由本博客收集整理于网络,只用于交流学习,请勿用作它途。如有侵权,请联系, 删除处理。
    注入点归纳
    网站入侵思路
    关于网上的“人肉”里面的技巧,简单解释
    SQL注入复习
    自己构造注入点方便入侵
    SQL通常注射的一些介绍
    AWVS13破解版安装_Windows
    黑页
    显ipQQ
  • 原文地址:https://www.cnblogs.com/vi3nty/p/7484794.html
Copyright © 2011-2022 走看看