Algorithm title week3

21 # merge two ordered lists

The two ordered lists into a new sorted list and return. The new list is by all nodes in a given mosaic composed of two lists. 

Example:

Input: 1-> 2-> 4, 1-> 3-> 4
Output: 1-> 1-> 2-> 3-> 4-> 4

Idea: set up a new list, continue to find smaller elements from the l1 and l2 connected to the back, generating a new order list.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while(l1 != null && l2 != null){
            if(l1.val<l2.val){
                cur.next = l1;
                cur = cur.next;
                l1 = l1.next;
            }
            else{
                cur.next = l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        if(l1 == null){
            cur. next = l2;
        }
        else
            cur.next = l1;
        return dummyHead.next;
    }
    
}

26 # delete duplicate entries sorted array

Given a sorted array, you need to remove the recurring elements in place, so that each element appears only once, after returning the new length of the array is removed.

Do not use extra space for an array, you must modify the input array in place and completed under the conditions of use O (1) extra space.

Thinking: Dual speed pointer points to the end will not be repeated a number has been found, a pointer to the original array, a pointer to find a different place slowly at the same speed to find the pointer continues to move rearwardly.

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums==null||nums.length==1){
            return nums.length;
        }
        else{
            int i = 0,j = 1;
            while(j<nums.length){
                if(nums[i]==nums[j]){
                    j++;
                }
                else{
                    i++;
                    nums[i] = nums[j];
                    j++;
                }
            }
            return i+1;
        }
    }
}

Removes the element # 27

Given an array nums and a value val, you need to place the elements to remove all equal values ​​val, and returns the new length of the array after removal.

Do not use extra space for an array, you must modify the input array in place and completed under the conditions of use O (1) extra space.

Order of the elements may be changed. You do not need to consider beyond the elements of the new array length behind.

Ideas: speed pointer

class Solution {
    public int removeElement(int[] nums, int val) {
        if(nums == null||(nums.length==1&&nums[0]!=val))
            return nums.length;
        int j = 0;
        for(int i =0;i<nums.length;i++){
            if(nums[i]!=val){
                nums[j]=nums[i];
                j++;
            }
        }
        return j;   
    }
}

 

Guess you like

Origin www.cnblogs.com/XinL-blog/p/11610222.html