LeetCode刷題——10th

難度:簡單/Easy

序號與難度:83——刪除排序鏈表的重複元素

給定一個排序鏈表,刪除所有重複的元素,使得每個元素只出現一次。

示例 1:
輸入: 1->1->2
輸出: 1->2

示例 2:
輸入: 1->1->2->3->3
輸出: 1->2->3

思考:由於輸入的鏈表已排序,因此我們可以通過將結點的值與它之後的結點進行比較來確定它是否爲重複結點。如果它是重複的,我們更改當前結點的 next 指針,以便它跳過下一個結點並直接指向下一個結點之後的結點。

實現:

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) 
{
     struct ListNode *current = head;
     while (current != NULL && current->next != NULL) 
     {
        if (current->next->val == current->val) 
        {
            current->next = current->next->next;
        } 
        else 
        {
            current = current->next;
        }
    }
        return head;
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution 
{
public:
    ListNode* deleteDuplicates(ListNode* head) 
    {
        struct ListNode *current = head;
        while (current != NULL && current->next != NULL) 
        {
            if (current->next->val == current->val) 
            {
                current->next = current->next->next;
            } 
            else 
            {
                current = current->next;
            }
        }
        return head;
    }
};

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution 
{
    public ListNode deleteDuplicates(ListNode head) 
    {
        ListNode current = head;
        while (current != null && current.next != null) 
        {
            if (current.next.val == current.val) 
            {
                current.next = current.next.next;
            } 
            else 
            {
                current = current.next;
            }
        }
        return head;
    }
}

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        tmp=ListNode(0)
        tmp.next=head
        while head:
            while head.next and head.next.val==head.val:
                head.next=head.next.next
            head=head.next
        return tmp.next

序號與題目:88——合併兩個有序數組

給定兩個有序整數數組 nums1 和 nums2,將 nums2 合併到 nums1 中,使得 num1 成爲一個有序數組。

說明:

初始化 nums1 和 nums2 的元素數量分別爲 m 和 n。
你可以假設 nums1 有足夠的空間(空間大小大於或等於 m + n)來保存 nums2 中的元素。
示例:

輸入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

輸出: [1,2,2,3,5,6]

思考:先將nums2的元素合併到nums1中,再對nums1中m+n-1個元素進行冒泡排序

實現:

C

void merge(int* nums1, int m, int* nums2, int n) 
{
    int tmp=0;
    for(int i=0;i<n;i++)
    {
        nums1[m+i]=nums2[i];
    }
    for(int i=0;i<m+n;i++)
	{
		for(int k=i+1 ;k<m+n ;k++)
		{
			if(nums1[i]>nums1[k])
			{
				tmp=nums1[i];
				nums1[i]=nums1[k];
				nums1[k]=tmp;
			}
        }	
    }      
}

C++

class Solution 
{
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 
    {
        int tmp=0;
        for(int i=0;i<n;i++)
        {
            nums1[m+i]=nums2[i];
        }
        for(int i=0;i<m+n;i++)
	    {
		    for(int k=i+1 ;k<m+n ;k++)
		    {
			    if(nums1[i]>nums1[k])
			    {
				    tmp=nums1[i];
				    nums1[i]=nums1[k];
				    nums1[k]=tmp;
			    }
            }	
        }      
    }
};

Java

class Solution 
{
    public void merge(int[] nums1, int m, int[] nums2, int n) 
    {
        int tmp=0;
        for(int i=0;i<n;i++)
        {
            nums1[m+i]=nums2[i];
        }
        for(int i=0;i<m+n;i++)
	    {
		    for(int k=i+1 ;k<m+n ;k++)
		    {
			    if(nums1[i]>nums1[k])
			    {
				    tmp=nums1[i];
				    nums1[i]=nums1[k];
				    nums1[k]=tmp;
			    }
            }	
        }      
    }
}

Python

class Solution:
    def merge(self, nums1, m, nums2, n):
        """
        :type nums1: List[int]
        :type m: int
        :type nums2: List[int]
        :type n: int
        :rtype: void Do not return anything, modify nums1 in-place instead.
        """   
        for i in range(n):
            nums1[m+i] = nums2[i]
        for i in range(len(nums1) - 1):  # 這個循環負責設置冒泡排序進行的次數
            for j in range(len(nums1) - i - 1):  # j爲列表下標
                if nums1[j] > nums1[j + 1]:
                    nums1[j], nums1[j + 1] = nums1[j + 1], nums1[j]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章