zoukankan      html  css  js  c++  java
  • LC.26.Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

    Given nums = [0,0,1,1,1,2,2,3,3,4],

    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

    It doesn't matter what values are set beyond the returned length.

     1 public int removeDuplicates(int[] nums) {
     2         if (nums == null || nums.length <=1){
     3             return nums.length ;
     4         }
     5         /*because its sorted
     6         * return [0, slow)
     7         * */
     8         //slow pointer: 启示的位置 从1 开始
     9         int slow = 1 ;
    10         //fast pointer
    11         /*
    12                          s
    13                     0,   0,   1,    1,   1,    2,    2,    3,   3,   4
    14                          i
    15         * */
    16         for (int i = 1; i < nums.length; i++) {
    17             if (nums[slow-1] != nums[i]){
    18                 nums[slow++] = nums[i];
    19             }
    20         }
    21         return slow;
    22     }

    需要和 LC.80.Remove Duplicates from Sorted Array II    对比参考

  • 相关阅读:
    [离散数学]集合3.1、3.2、3.3
    Hadoop YARN ResourceManager 未授权访问漏洞复现
    dns域传送漏洞
    文件包含漏洞详解
    XXE漏洞详解
    常见cms
    IP地址详解
    帧详解
    IP包头内容详解
    SSRF漏洞详解
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8859327.html
Copyright © 2011-2022 走看看