Code Signal_练习题_Sort by Height
Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!
Example
For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should besortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].
我的解答:
def sortByHeight(a):
j = -2
y = 0
li = []
if -1 not in a:
a = sorted(a)
else:
for i in a:
if i == -1:
pass
else:
li.append(i)
a[a.index(i)] = j
j -= 1
li = sorted(li)
for x in a:
if x != -1:
a[a.index(x)] = li[y]
y += 1
return a
感觉自己总是不按常规出牌
膜拜大佬:
def sortByHeight(a):
l = sorted([i for i in a if i > 0])
for n,i in enumerate(a):
if i == -1:
l.insert(n,i)
return l
Code Signal_练习题_Sort by Height的更多相关文章
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- netstat 查看本机开放端口
root@kali:~# netstat -luntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Add ...
- GPS欺骗(一)—无人机的劫持
本文作者:唯念那抹瑞利蓝 今天我们所讲的是GPS欺骗的方式和简单的定义.让大家对GPS欺骗这个方面有所了解.GPS是全世界地一个卫星定位系统,由美国制造. 0×01 例子2011年伊朗劫持美国无人机 ...
- iOS 模拟不同的字体大小
 真的是神器!! 参考 Creating Self-Sizing Table View Cells
- PriorityQueue实现大顶堆
在做一道算法时需要使用大顶堆,所以查了一下记录. 使用PriorityQueue实现大顶堆 PriorityQueue默认是一个小顶堆,然而可以通过传入自定义的Comparator函数来实现大顶堆.如 ...
- System.Thread.TImer控件——http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml
http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml
- 【学习笔记】linux bash script
1. sed sed 是一种流编辑器,它是文本处理中非常常用的工具,能够完美的配合正则表达式使用,功能非常强大. mkdir playground touch test.txt echo " ...
- Eclipse无法编译 build无效 没有class文件
问题原因: 我遇到这个问题的原因是: maven 插件引起的,maven clean或maven build后,经常无法自动编译class(虽然project自动编译了,但是只有包文件夹名,而没有cl ...
- 神策Loagent数据收集 windows部署的坑
部署可以修改bin文件夹下的bat文件.. java改为javaw..无窗口运行 重新启动的时候..要保证上次运行到的日志文件要还在..或者同名文件.. 保证要比之前的文件大些..所以最好是之前的文件 ...
- (转) mysqldumpslow使用说明总结
原文:http://blog.csdn.net/langkeziju/article/details/49301993 mysqldumpslow使用说明mysqldumpslow --helpUsa ...
- Java虚拟机(二):JVM内存模型
所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢?其实如果你经常解决服务器性能问题,那么这些问 ...