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的一个博客搬 ...
随机推荐
- CodeForces - 869A The Artful Expedient
题意:有两个序列X和Y,各含n个数,这2n个数互不相同,若满足xi^yj的结果在序列X内或序列Y内的(xi,yj)对数为偶数,则输出"Karen",否则输出"Koyomi ...
- 51nod 1441:士兵的数字游戏
1441 士兵的数字游戏 题目来源: CodeForces 基准时间限制:4 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 取消关注 两个士兵正在玩一个游戏,游戏开始的时 ...
- bootstrap点击下拉菜单没反应
出现这个问题一般就涉及 网页脚本的问题 好好看看自己网页 scripts 编写是否正确 也可以通过浏览器的 F12 进入console 控制台看看是什么问题 总的来说 该错误要从网页脚本编写的问题出发 ...
- zigbee CC2530首选方案模组:TZU06A1
模块特点 微型24-pin 邮票式SMT 封装 提供U.FL 接口,用于外接SMA 天线 小尺寸封装:16mm*20mm*3.7mm 通过欧盟CE0168.欧盟ROHS 认证 基于8051 单片机架构 ...
- C++代做,C++编程代做,C++程序代做,留学生C++ Lab代写
C++代做,C++编程代做,C++程序代做 我们主要面向留学生,广泛接美加澳国内港台等地编程作业代写,中英文均可. C语言代写 C++代写 Python代写 Golang代写 Java代写 一年半的时 ...
- php phar反序列化任意执行代码
2018年 原理一.关于流包装stream wrapper大多数的文件操作允许使用各种URL协议去访问文件路径,如data://,zlib://,php://例如常见的有include('php:// ...
- thinkphp配置到二级目录,不配置到根目录,访问除首页的其他路径都是404报错
1.在nginx的配置里面,进行重定向 vi /etc/nginx/conf.d/default.conf 2.进入编辑 location /thinkphp/public { if (!-e $re ...
- uni-app开发小程序-使用uni.switchTab跳转后页面不刷新的问题
uni.switchTab({ url: '/pages/discover/discover', success: function(e) { var page = getCurrentPages() ...
- (win32)解决虚拟按键被输入法截获(转)
源博客地址:http://blog.csdn.net/kencaber/article/details/51417871 响应WM_KEYDOWN消息时发现`~快捷键无效,设置断点发现得到的按键消息根 ...
- 小程序开发顶部TAB栏和侧边分类点击
先上一个效果图: 根据这个效果图我来说内容. 首先是顶部tab栏 效果实现依靠的是一个组件scroll-view.这个组件很有意思,可以多层嵌套,当然它的属性也很多. 这里主要用的是scroll-x, ...