• leetcode406 ,131,1091 python


    LeetCode 406. Queue Reconstruction by Height 解题报告
    题目描述
    Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. 



    The number of people is less than 1,100.

    示例
    Input:
    [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

    Output:
    [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

     


    def test(people):
    people_dict = {}
    for p in people:
    h,k = p[0],p[1]
    people_dict.setdefault(h,[])
    people_dict[h].append(k)
    print(people_dict)
    result = []
    for h in sorted(people_dict.keys(),reverse=True):
    print(h)
    people_dict[h].sort()
    for k in people_dict[h]:
    result.insert(k,[h,k])
    return result

    array = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
    t = test(array)
    print(t)

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

    返回 s 所有可能的分割方案。

    示例:

    输入: “aab”
    输出:
    [
    [“aa”,“b”],
    [“a”,“a”,“b”]
    ]
    思路
    用i遍历数组各个位置,如果s[:i]为回文串,则对后半部分进行递归,将返回的结果中每一项加上s[:i]并将该项添加至最终结果

     
    def partition(s):
    if (s == ''):
    return []
    e = []
    if (s == s[::-1]): e.append([s])
    for i in range(len(s)):
    if (s[:i + 1] == s[i::-1]):
    p = partition(s[i + 1:])
    for c in p:
    if (c != []):
    e.append([s[:i + 1]] + c)
    return e
    t=partition('aabb')
    print(t)


    在一个 N × N 的方形网格中,每个单元格有两种状态:空(0)或者阻塞(1)。

    
    

    一条从左上角到右下角、长度为 k 的畅通路径,由满足下述条件的单元格 C_1, C_2, ..., C_k 组成:

    
    

    相邻单元格 C_i 和 C_{i+1} 在八个方向之一上连通(此时,C_i 和 C_{i+1} 不同且共享边或角)
    C_1 位于 (0, 0)(即,值为 grid[0][0])
    C_k 位于 (N-1, N-1)(即,值为 grid[N-1][N-1])
    如果 C_i 位于 (r, c),则 grid[r][c] 为空(即,grid[r][c] == 0)
    返回这条从左上角到右下角的最短畅通路径的长度。如果不存在这样的路径,返回 -1 。

    
    

    示例 1:

    输入:[[0,1],[1,0]]
    输出:2
    

    示例 2:

    输入:[[0,0,0],[1,1,0],[1,1,0]]
    输出:4


    import collections
    class Solution(object):
    def shortestPathBinaryMatrix(self, grid):

    if len(grid)==0:
    return -1
    if grid[0][0]!=0:
    return -1
    q = collections.deque()
    q.append([0,0])

    visited = [[0 for i in range(len(grid[0]))] for j in range(len(grid))]
    visited[0][0] = 1

    while q:
    cur_x,cur_y = q.popleft()
    level = visited[cur_x][cur_y]
    dx = [0,0,-1,1,-1,1,-1,1]
    dy = [1,-1,0,0,1,-1,-1,1]

    for i in range(8):
    new_x = cur_x + dx[i]
    new_y = cur_y + dy[i]
    if new_x<0 or new_x>=len(grid) or new_y<0 or new_y>=len(grid[0]) or visited[new_x][new_y]!=0 or grid[new_x][new_y]!=0:
    continue
    q.append([new_x,new_y])
    visited[new_x][new_y] = level + 1
    if visited[-1][-1]!=0:
    return visited[-1][-1]
    else:
    return -1
    s = Solution()
    print(s.shortestPathBinaryMatrix([[0,1],[1,0]]))

    169.求众数
    描述

    给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
    你可以假设数组是非空的,并且给定的数组总是存在众数。

    示例

    输入: [3,2,3]
    输出: 3
    输入: [2,2,1,1,1,2,2]
    输出: 2

    
    
    def test(array):
    if len(array)==0:
    return -1
    dict = {}
    for i in array:
    if i in dict:
    dict[i]=dict[i]+1
    else:
    dict[i] = 1

    dict1 = sorted(dict.items(), key=lambda x: x[1], reverse=True)
    return dict1[0][1]


    array = [3,2,3,4,4,4,5,2]
    t = test(array)
    print(t)


  • 相关阅读:
    GCD
    常用存储位置
    AFNetworking 不支持 text/plain,unacceptable content-type: text/plain
    iOS layer 动画
    自定义对象实现copy,遵守协议<NSCopying, NSMutableCopying>
    Objective-c学习笔记03——内存管理
    Objective-c学习笔记02——类(面向对象)01
    Objective-c学习笔记01——简介
    Objective-c——NSString
    开源网站
  • 原文地址:https://www.cnblogs.com/limingqi/p/12284879.html
Copyright © 2020-2023  润新知