LeetCode 刷题总结 (array)-----1

最近准备着手刷题了,准备记录一下题解思路、具体代码,discussion中一些优秀的解决方案以及学到的一些知识点。每天都要有进步鸭!

刷题顺序:根据Tags分类,Lists中的 top interview Questions,难易程度是由easy到hard。

Q283. move zeros https://leetcode.com/problems/move-zeroes/

(将arrary中的0移动至数组尾部,不改变非零值的相对位置,不能copy原array)

idea:

使用python3,找到所有0元素的下标—记录0元素的个数—删除0元素—在list末尾追加n个0.(不够简便)

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        index_zero = [i for i in range(len(nums)) if nums[i]==0]
        len_zero = len(index_zero)
        while len_zero > 0:
            del nums[index_zero[len_zero-1]]
            len_zero-=1
        zero_list = [0]*len(index_zero)
        nums.extend(zero_list)

knowledge:

1. list删除元素有很多种方式,但是删除之后直接改变list内存数据。因此正序删除时,删除过程中其他元素的下标也会随之变动。可以采用从后往前删除,即倒序删除,在删除多个元素的情况下不容易发生bug。

2. 用重复元素初始化list:[0]*n 或者 list1 = [0 for i in range(5)]

discussion 解决方案1:

始终记录着第一个0元素(可能没有)的位置zero,将后面非零元素依次与zero处的0元素互换。

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        zero=0
        for i in range(len(nums)):
            if nums[i] != 0:
                nums[i],nums[zero] = nums[zero],nums[i]
                zero += 1

类似排序的思路,很巧妙!虽然运行时间比上面那个还要长一点。。。

discussion 解决方案2:

更加简便的倒序删除:

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        for i in range(len(nums)-1,-1,-1):
            if nums[i] == 0:
                nums.append(nums.pop(i))

利用 range(start,end,rate) 生成倒序数组。pop(index) 删除元素的同时返回该元素

 

Q169. Majority Element  https://leetcode.com/problems/majority-element/

(找到数组中出现次数>n/2的数,假设这个数一定存在且数组不为空)

idea:

list转set得到数组中有几种数据—对每一种数进行计数—取计数>n/2的数据—返回

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        num_set = set(nums)
        for i in num_set:
            count = nums.count(i)
            if count > len(nums)/2:
                return i
        

运行时间较慢

discussion解决方案1:

同样使用set,但在选择出现次数最多的数据时,采用的是记录最大数据的方法

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        num_set = list(set(nums))
        max_count = 0
        max_index = 0
        for i in num_set:
            count = nums.count(i)
            if count > max_count:
                max_count = count
                max_index = i
        return max_index

discussion解决方案2:

巧用list的sort方法,数组中只有唯一一个数据出现次数大于n/2,那么将数组进行排序之后,中位数一定是出现次数最多的那个数。

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return(nums[len(nums)/2])

list 的sort方法还可以自定义。list.sort(cmp=None, key=None, reverse=False)

cmp:可选参数, 如果指定了该参数会使用该参数的方法进行排序。

这个方法速度很快也很简便。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值