zoukankan      html  css  js  c++  java
  • remove-duplicates-from-sorted-array

    /**
    *
    * @author gentleKay
    * 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].
    *
    * 给定一个已排序的数组,将重复项移除到位,这样每个元素只出现一次,并返回新的长度。
    * 不要为另一个数组分配额外的空间,必须在内存恒定的情况下就地分配。
    * 例如,
    * 给定输入数组a=[1,1,2],
    * 您的函数应该返回length=2,而a现在是[1,2]。
    */

    /**
     * 
     * @author gentleKay
     * 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].
     * 
     * 给定一个已排序的数组,将重复项移除到位,这样每个元素只出现一次,并返回新的长度。
     * 不要为另一个数组分配额外的空间,必须在内存恒定的情况下就地分配。
     * 例如,
     * 给定输入数组a=[1,1,2],
     * 您的函数应该返回length=2,而a现在是[1,2]。
     */
    
    public class Main31 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int[] A = {1,1,1,2};
    		System.out.println(removeDuplicates(A));
    	}
    	
    	public static int removeDuplicates(int[] A) {
    		int count = 1;
    		for (int i=0;i<A.length-1;i++) {
    			if (A[i] != A[i+1]) {
    				A[count]=A[i+1];
    				count++;
    			}
    		}
    		return count;
        }
    
    }
    

      

  • 相关阅读:
    weiPHP微信开发框架
    win7系统
    csdn博客频道
    一步一步安装Git控件版本工具
    php源码,php网站源码,php源码下载
    czz数据专家
    禁用了传说中的PHP危险函数之后,Laravel的定时任务不能执行了?
    php禁用函数设置及查看方法详解
    laravel项目thinksns-plus安装出现RuntimeException Symlink from * to * failed错误
    laravel框架使用中错误及解决办法总结
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11301621.html
Copyright © 2011-2022 走看看