码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode - Remove Duplicates from Sorted Array

时间:2015-04-03 11:14:18      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

 

      删除数组里重复的元素,返回数组的元素个数。可以利用set集合元素不重复的特点来保存。

     

public class Solution {
    public int removeDuplicates(int[] A) {
        
        java.util.SortedSet<Integer> set = new java.util.TreeSet<>();
        for(int i=0; i<A.length; i++) {
        	set.add(A[i]);
        }
        
        int j = 0;
        for(int i : set) {
        	A[j++] = i;
        }
        
        return set.size();
    }
}

 

    

LeetCode - Remove Duplicates from Sorted Array

标签:

原文地址:http://www.cnblogs.com/wxisme/p/4389340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!