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 be
sortByHeight(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的更多相关文章

  1. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  2. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  3. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. 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) + ...

随机推荐

  1. C#里面获取web和非web项目路径

    非Web程序获取路径几种方法如下: 1.AppDomain.CurrentDomain.BaseDirectory  2.Environment.CurrentDirectory 3.HttpRunt ...

  2. php语法分析

    php的语法分析的主要作用是验证词法分析的基础上将token组成的序列,在php这门语言中是否是一个有效的句子,也可以理解为这些token序列是否匹配设计php这门语言时的语法模型,在匹配的情况下构建 ...

  3. Ubuntu安装PhpStorm并设置快速启动phpstorm

    使用sudo apt-get install phpstorm 安装php后,没有在桌面生成phpstorm的快捷方式,如果将phpstorm.sh的链接放到/usr/local/bin ,虽然可以从 ...

  4. spring中的context:include-filter和context:exclude-filter的区别

    在Spring 的配置文件中有: <context:component-scan base-package="njupt.dao,njupt.service"> < ...

  5. openssl生成RSA格式的公私钥,并转为pkcs8格式

    第一步:生成私钥,这里我们指定私钥的长度为2048 openssl genrsa -out rsa_private_key.pem 2048 第二步:根据私钥生成对应的公钥: openssl rsa ...

  6. phpstorm之自定义代码碎片(tab键自动填充代码)

    打开phpstorm 的设置界面(快捷键ctrl+alt+s) 比如上面的form表单,需要在生成以后自动跳转到“名称”的位置,然后更改,可以如下修改

  7. Postman—命令执行脚本及生成报告

    前言 前面的应用中,都是在postman图形界面工具里面进行测试,但是有时候我们需要把测试脚本集成到CI平台,或者在非图形界面的系统环境下进行测试,那么我们该如果处理呢 通过newman来执行post ...

  8. SearchView去掉下划线

    SearchView calSearchView = (SearchView) findViewById(R.id.sv_search_text); if (calSearchView != null ...

  9. PostgreSQL执行超时的问题

    PostgreSQL执行超时的问题 使用背景 最近在使用PostgreSQL的时候,在执行一些数据库事务的时候,先后出现了statement timetout 和idle-in-transaction ...

  10. java中的复制数组arraycopy()

    System.arraycopy();//静态方法,在System类中定义,注意copy首字母是小写的 例子: int[] a = {1,2,3,4,5}; int[] b = {9,8,7,6}; ...