Array-leetcode-python

这篇博客介绍了如何使用Python解决LeetCode上的数组问题,包括数组分组求最大和、删除元素、去重、查找重复元素以及寻找数组中第三大的数。解题策略涉及数组排序、双指针法和哈希表的应用。
摘要由CSDN通过智能技术生成

1、Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].


解析:题目的意思是给一个数组,相邻的数形成一个元组,计算每个元组最小的数之和的最大值。我们可以将数组进行排序,然后取下标为偶数的值相加。

    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        after_nums = sorted(nums)  //sorted(nums) 没有改变nums的值
        # print after_nums
        i = 0
        sum_max = 0
        while i < len(after_nums):
            sum_max += after_nums[i]
            i += 2
        return sum_max


2、Remove Element

Given an array and a value, remove all instances of that value in-place 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.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

 


解析:题目要求给定一个数组,删去指定的数之后,返回数组的长度,题目不建议新增一个数组,但可以改变原始数组的顺序。

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        j = len(nums)-1 # j指向数组末尾
        for i in range(len(nums)-1, -1, -1): # i从尾往前遍历
            if nums[i] == val:
                nums[i], nums[j] = nums[j], nums[i] # 若和目标值相等,则将i的值和j的值交换,j--
                j -= 1

        return j+1


3、Remove Duplicates from Sorted Array

Given a sorted array, 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.

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.


解析:给定一个排序的数组,要求删除重复的数字,返回删除后的长度,但题目又不允许使用额外的空间,因此本题思路引入两个指针,一个慢指针初始位于数组下标为0的位置,快指针初始位于下标为1的位置,快指针从前往后遍历,当遇到与其前面一个数字不相同的数,将其放置到i指针的后面,i++,j++

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        
        # 删除列表中的重复项,返回操作后的长度
        # [1,1,1,2,3,4,4,4,5] -> [1,2,3,4,5] 5
        # 维护2个索引,慢的s,快的f;s指向第一个元素,f的指向第二个元素;
        # 判断f和f前一个元素是否相等,相等则f后移;不等则s后移一个,值给s,然后f也后移
        if len(nums) <= 1:
            return len(nums)


        s = 0
        # 循环的作用是将不重复的数放置到数组的前面
        for f in range(1, len(nums)): # 快指针从下标1开始往后遍历
            if nums[s] != nums[f]: # 如果慢指针的数不等于快指针所指的数
                s += 1
                nums[s] = nums[f] # 将快指针所指的数放在慢指针的后一位

        return s + 1

4、 Contains Duplicate II

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false

判断数组是否存在不同下标,数值相同,且下标相差小于k的情况

关键在于使用hash表,也就是python的dict


5、求数组中第三大的数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值