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

    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 A = [1,1,2],

    Your function should return length = 2, and A is now [1,2].

    注意是已经排好序的array, 再就是len 要+1。

     1 public class Solution {
     2     public int removeDuplicates(int[] A) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         if(A == null || A.length == 0) return 0;
     5         if(A.length == 1) return 1;
     6         int start = 0;
     7         for(int i = 0; i < A.length; i ++){
     8             if(A[i] != A[start]){
     9                 A[++start] = A[i];
    10             }
    11         }
    12         return ++start;
    13     }
    14 }
  • 相关阅读:
    块级元素和内联元素
    cookie和session 区别
    hihernate一对多关联映射
    --查询50到80行数据
    单表查询
    表空间 用户
    进程遍历模块遍历
    字符转换函数
    控件综合2
    清理文件2
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3370131.html
Copyright © 2011-2022 走看看