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)
leetcode406 ,131,1091 python的更多相关文章
- 一些随机数据的生成(日期,邮箱,名字,URL,手机号,日期等等)
直接上代码 import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import jav ...
- python的高级特性:切片,迭代,列表生成式,生成器,迭代器
python的高级特性:切片,迭代,列表生成式,生成器,迭代器 #演示切片 k="abcdefghijklmnopqrstuvwxyz" #取前5个元素 k[0:5] k[:5] ...
- 执行Django数据迁移,报错 1091
问题描述 今天在Pycharm 中的Terminal下,执行数据迁移操作时,第一步: Python manage.py makemigrations ,是没有任何问题,但就是在执行真正的数据迁移时,也 ...
- python征程3.1(列表,迭代,函数,dic,set,的简单应用)
1.列表的切片. 1.对list进行切片.'''name=["wangshuai","wangchuan","wangjingliang", ...
- (八)map,filter,flatMap算子-Java&Python版Spark
map,filter,flatMap算子 视频教程: 1.优酷 2.YouTube 1.map map是将源JavaRDD的一个一个元素的传入call方法,并经过算法后一个一个的返回从而生成一个新的J ...
- win7系统下python安装numpy,matplotlib,scipy和scikit-learn
1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...
- python 函数之装饰器,迭代器,生成器
装饰器 了解一点:写代码要遵循开发封闭原则,虽然这个原则是面向对象开发,但也适用于函数式编程,简单的来说,就是已经实现的功能代码不允许被修改但 可以被扩展即: 封闭:已实现功能的代码块 开发:对扩张开 ...
- python学习之day5,装饰器,生成器,迭代器,json,pickle
1.装饰器 import os import time def auth(type): def timeer(func): def inner(*args,**kwargs): start = tim ...
- python中文字符乱码(GB2312,GBK,GB18030相关的问题)
转自博主 crifan http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ 在玩wordpress的一个博客搬 ...
随机推荐
- python-python基础2
本章内容: 1.列表.元组 2.字典 3.集合 4.文件操作 5.字符编码与转码 一.列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 names= ...
- greenplum 数组操作
参考:http://gpdb.docs.pivotal.io/4390/admin_guide/query/topics/functions-operators.html Table 4. Advan ...
- P1087 有多少不同的值
P1087 有多少不同的值 转跳点:
- CodeForces - 869C The Intriguing Obsession(组合数)
题意:有三个集合,分别含有a.b.c个点,要求给这些点连线,也可以全都不连,每两点距离为1,在同一集合的两点最短距离至少为3的条件下,问有多少种连接方案. 分析: 1.先研究两个集合,若每两个集合都保 ...
- 数十万PhpStudy用户被植入后门,快来检测你是否已沦为“肉鸡”!
北京时间9月20日,杭州公安发布<杭州警方通报打击涉网违法犯罪暨‘净网2019’专项行动战果>一文,文章曝光了国内知名PHP调试环境程序集成包“PhpStudy软件”遭到黑客篡改并植入“后 ...
- 040、Java中逻辑运算之短路与运算“&&”
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- centos7下安装maven
步骤1:在home目录下解压apache-maven-3.5.0-bin.tar.gz安装包 [root@model ~]# -bin.tar.gz 步骤2:创建/maven目录并将解压后的文件夹移至 ...
- maven package和install
mvn clean package依次执行了clean.resources.compile.testResources.testCompile.test.jar(打包)等7个阶段.mvn clean ...
- 如何用naviecat批量创建mysql数据
1.参考博文:https://blog.csdn.net/lelly52800/article/details/87267096 2.excel要与表结构一致 3.右键,导入向导,选择相应版本,点击“ ...
- linux下anaconda的安装和使用
1.将python3设置为默认 直接执行这两个命令即可: sudo update-alternatives --install /usr/bin/python python /usr/bin/pyth ...