Array-leetcode-python

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

Array-leetcode-python

5、求数组中第三大的数

Array-leetcode-python