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的一个博客搬 ...
随机推荐
- 【Unity】鼠标指向某物体,在其上显示物体的名字等等等等信息
之前一直用NGUI HUD Text插件做这个功能,感觉一个小功能就导一个插件进来简直丧心病狂.然后就自己写了一个~ Camera cam;//用于发射射线的相机 Camera UIcam;//UI层 ...
- 手动搭建Vue之前奏:搭建webpack项目
手动搭建vue项目 搭建vue前首先搭建webpack项目 首先你应当安装一下npm以及nodejs 安装完成后,进行如下操作: // 创建项目根目录 mkdir some_project_name ...
- 在CentOS 7环境下安装 Spark
1.下载Spark安装包:http://mirror.bit.edu.cn/apache/spark/ 2.解压Spark的安装包并更改名称: (1)tar -zxvf spark-2.4.3-bin ...
- python的super()以及父类继承
Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__ini ...
- Day8 - B - Non-Secret Cypher CodeForces - 190D
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their nativ ...
- Django 项目搭建
django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...
- LocalDateTime整合到SpringBoot + MyBatis中
啥也不说先贴两个代码: 一:MVC层配置 @Configuration public class JacksonConfig { /** 默认日期时间格式 */ public static final ...
- Kafka--windows下简单使用kafka命令
参考 https://www.cnblogs.com/cici20166/p/9426417.html 启动zookeeper 只需要保证有可用的zookeeper,可以使用kafka内置的,也可以自 ...
- Windows 10中使用VirtualBox
新版Windows 10或者安装了新的更新以后,VirtualBox虚拟机就不能用了. 原因是WIndows10里面有个叫Virtualization-base security的安全机制打开了. 关 ...
- ActorFramework教程对比及规划
牢骚太盛防肠断,风物长宜放眼量. 一.引子 昨天的文章,本来就是想写写ActorFramework的教程内容,结果写着写着偏了,变成了吐槽. 首先,声明一下,自己从未参加过任何LabVIEW培训班,也 ...