Leetcode(python3):数组一

代码1:Remove Duplicates from Sorted Array
(关键点在于已排序)(先参看的其他人c++的代码)

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return 0
        index = 0
        for i in range(1,len(nums)):
            if nums[index]!=nums[i]:
                index = index + 1
                nums[index] = nums[i]

        return index + 1

代码二:Remove Duplicates from Sorted Array II
一开始没有对nums长度判断,导致索引越界。应该直接判断 index-1的位置和i的值是不是相等,这样会减少判断。

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # if len(nums)==0:
        #     return 0
        if len(nums)<3:
            return len(nums)
        index = 1

        for i in range(2,len(nums)):

            if nums[index-1]!=nums[index]:
                index = index +1
                nums[index]=nums[i]

            elif nums[index]!=nums[i]:
                index = index +1
                nums[index]=nums[i]

        return index+1
class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # if len(nums)==0:
        #     return 0
        if len(nums)<3:
            return len(nums)
        index = 1

        for i in range(2,len(nums)):

#             if nums[index-1]!=nums[index]:
#                 index = index +1
#                 nums[index]=nums[i]

#             elif nums[index]!=nums[i]:
#                 index = index +1
#                 nums[index]=nums[i]

            if nums[index-1]!=nums[i]:
                index = index + 1
                nums[index] = nums[i]

        return index+1

代码3:Remove Duplicates from Sorted List
主要是对Python的链表结构的把握:
1:Python中没有明确的指针工具(C++),但是有非基本结构的对象的引用,表示指针。在Python中,任何变量都可以引用任何任何内容,包括None值:
2:链表结构中的基本单位是节点。Node{val:数据项的一个引用,next:下一个节点的引用}

总的来说,链接(link)和指针(pointer),和引用(reference)可以互换使用。
node1 = None
node2 = Node(“A”,None)
node3 = Node (“B”,node2)
其中
右边是实际定义的节点变量。
左边node1,node2,node3 都可以看做一个引用。

Given a sorted linked list, delete all duplicates such that each element appear only once.
# 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
        """
        # if head==None:
        #     return 0

        p = head
        while p:
            while p.next and p.val==p.next.val:

                p.next = p.next.next

            p = p.next


        return head


代码4Remove Duplicates from Sorted List II

有点错误的代码一直没有想通

# 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
        """


        p = head
        q = ListNode(-1)
        q.next = head
        index = 1


        while p:


            while p.next!=None and p.val ==p.next.val:
                p.next = p.next.next

                index = 0

            p = p.next

            if index==1:
                q = q.next

            if index==0:


                q.next = p

                index =1



        return head



上面那个在提交解决方案时候会报错,也就是考虑不全 。想的太长时间了,先放着,以后再解决。

下面正确答案,

# 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
        """

# 对python不熟悉,尤其是curr本身代表着什么,curr.next又代表着什么。

#和我自己写的那个代码,有几点的不一样是 我还想着先删掉后面那一个,然后利用前面的一个指针来操作,但是针对链表的第一个节点就重复了,而且在一开始定义p的前节点时候,出现了问题,
        dummy = ListNode(-1)
        dummy.next = head
        curr = dummy
        is_repeat = False
        while curr.next:
            while curr.next.next and curr.next.val == curr.next.next.val:
                curr.next = curr.next.next
                is_repeat = True
            if is_repeat:
                curr.next = curr.next.next
                is_repeat = False
            else:
                curr = curr.next
        return dummy.next



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值