Leetcode sort の simple questions training set (242 349 922 976 1030) python

Disclaimer: This article is original, All Rights Reserved https://blog.csdn.net/weixin_41864878/article/details/90634262

Fast row

I did not fast before handwriting row, today practice a little, but soon exhaust itself in fact, there are many optimization techniques, it reserved TODO
I was trained on the 1030 title, the findings and the direct use of python built-in sort () time is as fast (??)

       def quick_sort(dis, start, end):
            if start < end:
                i = start
                j = end
                tmp = dis[i]
                while i < j:
                    while i < j:
                        if dis[j] < tmp: 
                            dis[i] = dis[j]
                            break
                        j -= 1
                    while i < j:
                        if dis[i] > tmp:
                            dis[j] = dis[i]
                            break
                        i += 1
                dis[i] = tmp
                quick_sort(dis, start, i-1)
                quick_sort(dis, i+1, end)
            return dis

        dis = quick_sort(dis, 0, len(dis) - 1)

Here Insert Picture Description

242. Effective ectopic letter word

Given two strings s and t, t write a function to determine whether the ectopic letters of the word s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
Example 2:

Input: s = "rat", t = "car"
Output: false
Note: You can assume string contains only lowercase letters.

Advanced: If the input string contains unicode characters how to do? Can you adjust your solution to deal with this situation?

Title meaning is to look at these two strings in the number of each letter is not as much, then you can sort one contrast
built dictionary I use here is mainly read reviews, think this is really very elegant method of construction dictionary very pythonic wow ~ ~ ~

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        map_s, map_t = {}, {}
        for i in s:
            map_s[i] = map_s.get(i, 0) + 1
        for i in t:
            map_t[i] = map_t.get(i, 0) + 1
        return map_s == map_t  

349. The intersection of two arrays

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:

Input: = [4,9,5], nums2 = [9,4,9,8,4] nums1
Output: [9,4]
Description:

The output of each element must be unique. We can not consider the order of output.

get a little addicted to the dictionary, but still not sorted and what relationship

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        map1 = {}
        res = []
        for i in nums1:
            map1[i] = map1.get(i, 0)
        for i in nums2:
            if i in map1:
                res.append(i)
        return set(res)
        ###只用一行的写法
        # return list(set(nums1) & set(nums2))

Here Insert Picture Description

922. The odd and even sorted array II

Given a non-negative integer array A, the A half of the integer is an odd number, an even number of half integer.

Sort the array, such that when A [i] is an odd number, I is an odd number; when A [i] is an even number, I is an even number.

You can return any array satisfy the above conditions as the answer.

Example:

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4 5] it will be accepted.

prompt:

2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000

And did anything to sort
idea is to use the method stack, stack and even the odd stack, and then turn pop out

class Solution(object):
    def sortArrayByParityII(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        map_odd, map_even = [], []
        for i in A:
            if i % 2 == 0:
                map_even.append(i)
            else:
                map_odd.append(i)
        res = []
        for i in range(0, len(A), 2):
            res.append(map_even.pop())
            res.append(map_odd.pop())
        return res

976. triangular maximum perimeter

A given array by a number of positive (representative length) consisting of return, the area of ​​the triangle is not zero from the maximum perimeter length of which three thereof.

If not form any non-zero triangular area, 0 is returned.

Example 1:

Input: [2,1,2]
Output: 5
Example 2:

Input: [1,2,1]
Output: 0
Example 3:

Input: [3,2,3,4]
Output: 10
Example 4:

Input: [3,6,2,3]
Output: 8

prompt:

3 <= A.length <= 10000
1 <= A[i] <= 10^6

This is a mathematical proofs, arranged for a small to large number of array [a, b, c] ( a <b <c) , if a + b <= c, then, is less than a, b of when a certain number of impossible and c composed of triangles, and reverse search, the first three meets a number of conditions must be the circumference of the largest triangle
next time I must carefully consider the line and hand sorting algorithm 1551, sort () harm ......

class Solution(object):
    def largestPerimeter(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        A.sort()
        for i in range(len(A)-1, 1, -1):
            if A[i-2] + A[i-1] > A[i]: 
                return A[i-2] + A[i-1] + A[i]
        return 0 

Here Insert Picture Description

From the order of 1030. Cell Matrix

Given matrix R rows and C columns, where the cell is integer coordinates (r, c), satisfying 0 <= r <R and 0 <= c <C.

In addition, we give a cell coordinates (r0, c0) in the matrix.

Returns the coordinates of all cells in the matrix, according to (r0, c0) of the distance from the smallest to the largest arranged in order, wherein a distance between the two cell (r1, c1) and (r2, c2) Manhattan distance, | r1 - r2 | + | c1 - c2 |. (You can return to answer any order to satisfy this condition.)

Example 1:

Input: R = 1, C = 2 , r0 = 0, c0 = 0
Output: [[0,0], [0,1]]
Explanation: From (r0, c0) to the distance to other cells: [0 , 1]
example 2:

Input: R = 2, C = 2 , r0 = 0, c0 = 1
Output: [[0,1], [0,0], [1,1], [1,0]]
Explanation: From (r0, c0) to the distance to other cells: [0,1,1,2] [[0,1], [1,1], [0,0], [1,0]] will be deemed correct and answer.
Example 3:

Input: R = 2, C = 3 , r0 = 1, c0 = 2
Output: [[1,2], [0,2], [1,1], [0,1], [1,0], [0,0]]
explanation: from (r0, c0) to the distance to other cells: [0,1,1,2,2,3] satisfy other requirements of the subject will be as correct answer, for example [ [1,2], [1,1], [0,2], [1,0], [0,1], [0,0]].

prompt:

1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C

Maintain a list of records from the
maintenance of a dictionary to record the coordinates of the same distance from the point
and then traverse the output dictionary

class Solution(object):
    def allCellsDistOrder(self, R, C, r0, c0):
        """
        :type R: int
        :type C: int
        :type r0: int
        :type c0: int
        :rtype: List[List[int]]
        """
        dis = []
        dis_map = {}
        for i in range(R):
            for j in range(C):
                d = abs(i-r0) + abs(j-c0)
                if d not in dis_map: 
                    dis.append(d)
                    dis_map[d] = []
                dis_map[d].append([i, j])
        dis.sort()
        res = []
        for i in dis:
            p = dis_map[i]
            for item in p:
                res.append(item)
        return res   

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_41864878/article/details/90634262