[Leetcode remove duplicate numbers in an ordered sequence] remove duplicates from sorted array (list) I (ii)

Source: Internet
Author: User

Leetcode has four questions about removing repeated numbers from an ordered sequence. Two of them are arrays and two are linked lists:

(1) remove duplicates from sorted array I: removes repeated numbers from an ordered array and returns the size of the new array.

(2) remove duplicates from sorted array II: removes duplicate numbers from an ordered array and returns the size of the new array. Different from the preceding question, each number can appear up to two times.

(3) remove duplicates from sorted list I: removes duplicate numbers from an ordered linked list and returns the first node.

(4) remove duplicates from sorted List II: Remove a node with duplicate numbers in an ordered linked list and return the first node.

The following four questions are analyzed separately.


Question 1: 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 shocould return length =2, And a is now[1,2].

Resolution: removes repeated numbers from an ordered array and returns the size of the new array. It is easy to use a temporary variable temp to record the value of the current array, compare the next value with this value, and determine whether it can enter the new array.

The Java AC code is as follows:

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

Question 2: remove duplicates from sorted array II

Follow up for "remove duplicates ":
What if duplicates are allowed at most twice?

For example,
Given sorted array A =[1,1,1,2,2,3],

Your function shocould return length =5, And a is now[1,1,2,2,3].

Resolution: Remove duplicate numbers from an ordered array and return the size of the new array. Different from the previous question, each number can be repeated up to two times. Therefore, you can add a Boolean variable ifrepeated to record whether there are repeated numbers.

Java AC code:

public class Solution {public int removeDuplicates(int[] A) {if (A == null || A.length == 0) {return 0;}boolean ifRepeated = false;int curPos = 0;for (int i = 1; i < A.length; i++) {if (A[i] != A[curPos]) {ifRepeated = false;curPos++;A[curPos] = A[i];} else if (!ifRepeated) {ifRepeated = true;curPos++;A[curPos] = A[i];}}return curPos + 1;}}

Question 3: remove duplicates from sorted list

Given a sorted Linked List, delete all duplicates such that each element appear only once.

For example,
Given1->1->2, Return1->2.
Given1->1->2->3->3, Return1->2->3.

Resolution: Remove duplicate numbers from an ordered linked list and return the first node. The main idea of this question is to use two nodes pre and cur to record the end nodes of the completed linked list and the nodes currently traversed. Note: If the current node cur is a duplicate node, set pre. Next to null.

The Java AC code is as follows:

public class Solution {public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode pre = head;ListNode cur = pre.next;while (cur != null) {if (cur.val != pre.val) {pre.next = cur;pre = cur;}else{pre.next = null;}cur = cur.next;}return head;}}
Question 4: remove duplicates from sorted List II

Given a sorted Linked List, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given1->2->3->3->4->4->5, Return1->2->5.
Given1->1->1->2->3, Return2->3.

Resolution: Remove a node with duplicate numbers in an ordered linked list and return the first node. If the header node may not be in the output linked list, we recommend that you use "dummy node" dummy for marking. Pre and cur respectively record the two front and back nodes. If the values of the two nodes are different, the PRE node can be added to the result linked list based on the Boolean variable ifdulipcated. If the values of the two nodes are the same, you need to mark ifdulipcated as true.

Note: When the pre node cannot be added to the result linked list, use point. Next = NULL to disconnect the current node.

Java AC code:

public class Solution {        public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode pre = head;ListNode cur = pre.next;ListNode point = new ListNode(-1);ListNode dummy = point;boolean ifDuplicated = false;while (cur != null) {if (cur.val != pre.val) {if (!ifDuplicated) {point.next = pre;point = point.next;} else {point.next = null;}ifDuplicated = false;} else {point.next = null;ifDuplicated = true;}pre = pre.next;cur = cur.next;}if (!ifDuplicated) {point.next = pre;}return dummy.next;}}



[Leetcode remove duplicate numbers in an ordered sequence] remove duplicates from sorted array (list) I (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.