NC_22_MERGE_ARRAY-NC_23_PARTITION_LIST-NC_24_DELETEDUPLICATENODE
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/9/1 */ public class NC_22_MERGE_ARRAY { public static void merge(int A[], int m, int B[], int n) { if (m == 0) { for (int i=0;i<B.length;i++) { A[i] = B[i]; } return ; } int i=0; int j = 0; for (;i<m&&j<n;) { if (A[i] < B[j]) { i++; } else { moveBack(A,i, m); A[i] = B[j]; j++; } } if (j<n) { int start = m + (n-j-1); for (;j<n;j++) { A[start] = B[j]; start++; } } } private static void moveBack(int arr[], int start,int validLength) { for (int i=start;i<validLength;i++) { arr[i+1] = arr[i]; } } public static void merge2(int A[], int m, int B[], int n) { int end = m + n - 1, i = m - 1, j = n - 1; //初始化 end,i,j while (end != 0 && i >= 0 && j >= 0) { if (A[i] > B[j]) { A[end--] = A[i--]; //从尾部开始 } else { A[end--] = B[j--]; } } while (j >= 0) { A[end--] = B[j--]; // i《0,复制B } } public static void main (String[] agrs) { int[] a = new int[2]; a[0] = 2; int[] b = {1}; merge2 (a,1,b,1); } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/25 */ public class NC_23_PARTITION_LIST { public static class ListNode { int val; ListNode next = null; public ListNode(int val) { this.val = val; } } public ListNode partition (ListNode head, int x) { // write code here ListNode small = new ListNode(0); ListNode smallHead = small; ListNode large = new ListNode(0); ListNode largeHead = large; while (head != null) { if(head.val<x){ small.next = head; small = small.next; }else{ large.next = head; large =large.next; } head = head.next; } //对链表进行指空处理 large.next = null; //将链表连起来 small.next = largeHead.next; //返回新链表 return smallHead.next; } }
package org.example.interview.practice; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashSet; import java.util.Set; /** * @author xianzhe.ma * @date 2021/7/21 */ public class NC_24_DELETEDUPLICATENODE { public static class ListNode { int val; ListNode next = null; public ListNode(int val) { this.val = val; } } public static ListNode deleteDuplicates (ListNode head) { // write code here Deque<ListNode> deque = new ArrayDeque<>(); while (head != null) { if (deque.isEmpty() || deque.peekLast().val != head.val) { deque.addLast(head); head = head.next; } else { while (head != null && deque.peekLast().val == head.val) { head = head.next; } ListNode last = deque.removeLast(); last = null; } } head = null; ListNode cur = null; while (!deque.isEmpty()) { ListNode node = deque.pollFirst(); node.next = null; if (head == null) { head = node; cur = node; } else { cur.next = node; cur = cur.next; } } return head; } public static void main (String[] args) { ListNode node = new ListNode(1); ListNode two = new ListNode(2); node.next = two; ListNode three = new ListNode(2); two.next = three; ListNode result = deleteDuplicates(node); System.out.println(result.val); } }
package org.example.interview.practice; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashSet; import java.util.Set; /** * @author xianzhe.ma * @date 2021/7/21 */ public class NC_25_DELETEDUPLICATENODE_I { public static class ListNode { int val; ListNode next = null; public ListNode(int val) { this.val = val; } } public static ListNode deleteDuplicates (ListNode head) { // write code here Deque<ListNode> deque = new ArrayDeque<>(); while (head != null) { if (deque.isEmpty() || deque.peekLast().val != head.val) { deque.addLast(head); head = head.next; } else { while (head != null && deque.peekLast().val == head.val) { head = head.next; } // ListNode last = deque.removeLast(); // last = null; } } head = null; ListNode cur = null; while (!deque.isEmpty()) { ListNode node = deque.pollFirst(); node.next = null; if (head == null) { head = node; cur = node; } else { cur.next = node; cur = cur.next; } } return head; } public static void main (String[] args) { ListNode node = new ListNode(1); ListNode two = new ListNode(2); node.next = two; ListNode three = new ListNode(2); two.next = three; ListNode result = deleteDuplicates(node); System.out.println(result.val); } }