leetcode Remove DUplicates from Sorted List

来源:互联网 发布:大金焓湿图计算软件 编辑:程序博客网 时间:2024/06/05 10:18

题目

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

这是一个典型的题目

public class Solution {    public int removeDuplicates(int[] A) {        if(A==null)return 0;    if(A.length<=1)return A.length;        int begin=1;        int temp=A[0];                for(int i=1;i<A.length;i++){        if(temp!=A[i]){        A[begin++]=A[i];        temp=A[i];        }        }        return begin;    }}

还有一个是链表的删除重复元素

https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/

public class Solution {    public ListNode deleteDuplicates(ListNode head) {    if(head==null||head.next==null)return head; ListNode pre=head; ListNode p=head.next; while(p!=null){ if(p.val==pre.val){ pre.next=p.next; p=p.next; }else{ pre=p; p=p.next; } } return head;            }}


0 0
原创粉丝点击