【leetcode】1054. Distant Barcodes
题目如下:
In a warehouse, there is a row of barcodes, where the
i
-th barcode isbarcodes[i]
.Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
解题思路:首先把input按元素出现的次数从多到少排序,如果出现的次数一样,可以按值从小到大排好序。接下来遍历input,依次pop出input的第一个元素,并且把这个元素放入Output的奇数位上;完成之后再依次pop出input的第一个元素并放入Output的偶数位。
代码如下:
class Solution(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
dic = {}
for i in barcodes:
if i not in dic:
dic[i] = [i,1]
else:
dic[i][1] += 1 def cmpf(l1,l2):
if l1[1] != l2[1]:
return l2[1] - l1[1]
return l1[0] - l2[0] tmp_list = sorted(dic.itervalues(),cmp=cmpf) blist = []
for (k,v) in tmp_list:
blist += [k] * v res = [0] * len(barcodes)
for i in range(0,len(res),2):
res[i] = blist.pop(0)
for i in range(1,len(res), 2):
res[i] = blist.pop(0)
return res
【leetcode】1054. Distant Barcodes的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- 自定义控件 - 切换开关:SwitchView
自定义控件一般的几个步骤:1.初始化相关背景图片,布局文件,自定义属性2.设置控件宽高OnMeasure()3.布局或者排版OnLayout()4.绘制控件OnDraw()5.处理触摸事件OnTouc ...
- python文件操作错误解决
1. python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illega ...
- 用Vue来实现音乐播放器(十五):处理得到的歌手数据
之前得到的歌手数据是用forEach遍历添加的 没有顺序性 我们希望得到的数据是title:"热门"的数据在最上面 title为字母的数据按字母从低到高顺序排列 var ho ...
- 20160513--js 弹出窗口带有iframe控件 备忘
需要引用JQuery. /*! * 主 题:<页面弹出窗口> * 说 明:用于页面弹出的窗口. * 功能描述: * 1.生成弹出窗口,窗口内包括iframe控件: * 2.窗口弹出时,生成 ...
- tensorflow学习笔记二:入门基础 好教程 可用
http://www.cnblogs.com/denny402/p/5852083.html tensorflow学习笔记二:入门基础 TensorFlow用张量这种数据结构来表示所有的数据.用一 ...
- [Python3] 005 列表的基本使用
目录 1. 列表概述 2. 创建列表 3. 列表常用操作 (1) 访问列表 (2) 分片操作 1) 正向操作 2) 反向操作 3) 内置函数 id() 加入队伍 1. 列表概述 一组有顺序的数据的组合 ...
- C# DataSet转JSON
经常会遇到系统数据交互采用JSON数据格式进行交互的,避免不必要的重复工作,记录下自己的处理方式. 获取数据集之后,通过函数对数据集信息进行整理通过.Net Framework3.5提出的JavaSc ...
- java_第一年_JavaWeb(11)
自定义标签:主要是用来移除JSP页面中的java代码. 先从一个简单的案例了解其怎么移除代码: 一个正常的jsp页面: <%@ page language="java" pa ...
- java 注解 Annontation
什么是注解? 对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metada ...
- 找不到opencv_world320d.dll的问题
OpenCV执行时出现找不到opencv_world320d.dll的问题,解决办法:把自己opencv文件目录下的D:\opencv3.2.0\opencv\build\x64\vc14\bin(本 ...