Where do I belong
先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引。
举例:where([1,2,3,4], 1.5) 应该返回 1。因为1.5插入到数组[1,2,3,4]后变成[1,1.5,2,3,4],而1.5对应的索引值就是1。
同理,where([20,3,5], 19) 应该返回 2。因为数组会先排序为 [3,5,20],19插入到数组[3,5,20]后变成[3,5,19,20],而19对应的索引值就是2。
这是一些对你有帮助的资源:
思路就是先把数值插入数组,然后排序,找出插入的数在新数组的索引。 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
function where(arr, num) {
arr.push(num); //插入数组
return arr.sort(function(f,s){
return f-s; //排序从小到大
}).indexOf(num);
}
Where do I belong的更多相关文章
- 解决在使用client object model的时候报“object does not belong to a list”错误
在查看别人代码的时候,发现了个有意思的问题,使用client object model将一个文件check in 我使用的是如下语句获取file Microsoft.SharePoint.Client ...
- Cannot uninstall 'html5lib'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
如标题,安装Tensorflow-gpu时遇到的完整问题 Cannot uninstall 'html5lib'. It is a distutils installed project and th ...
- Cannot uninstall 'pyserial'. It is a distutils installed project and thus we cannot a ccurately determine which files belong to it which would lead to only a partial uninstall. 解决方法
最近再升级 pyserial模块时,采用 pip install --upgrade pyserial,待模块下载完成准备卸载原版本时 提示:“Cannot uninstall 'pyserial'. ...
- python Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
在Python中移除(升级)numpy的时候出现: Cannot uninstall 'numpy'. It is a distutils installed project and thus we ...
- All flavors must now belong to a named flavor dimension
FAQ: All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/ ...
- Error:All flavors must now belong to a named flavor dimension.
环境 android studio 3.0 错误 Error:All flavors must now belong to a named flavor dimension. 解决 在build.gr ...
- Cannot uninstall 'enum34'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
更新tensorflow时遇到报错 Found existing installation: enum34 1.0.4Cannot uninstall 'enum34'. It is a distut ...
- Country roads take me home, to the place I belong.
Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.
- ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
pip 安装 docker库报错: ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we ...
- 安卓 android studio 报错 All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译.于是我把我的gradle升级到了 gradle-4.1-milestone-1 版本,是 ...
随机推荐
- cdoj1580 简单图论问题
地址:http://acm.uestc.edu.cn/#/problem/show/1580 题目: 简单图论问题 Time Limit: 3000/1000MS (Java/Others) ...
- 【PS技巧】创建2D对象的描边阴影
在本场景中,怪物死亡掉落宝袋.所以在玩家眼里,宝袋是掉落在场景里,而不是像其他界面的UI元素,悬浮在场景上的. 所以,我们需要给宝袋添加阴影,增加它与场景之间的视觉过渡,比较简单的办法是使用阴影,正如 ...
- 如何释放linux cache占用的内存
[root@prd-fygl-app-01 ~]# free -m total used free shared buffers ...
- react-native android 打包发布
react-native android 打包步骤 <一>.生成签名文件(应用身份证) 1.使用keytool命令 keytool -genkey -v -keystore my-r ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- Linux中find
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- AJAX跨域问题解决方法(4)——调用方解决跨域
调用方解决跨域的方法只有一种,那就是隐藏跨域. 何为隐藏跨域? 隐藏跨域的核心思路是通过反向代理隐藏跨域以欺骗浏览器 什么是反向代理?反向代理是指通过中间服务器使得访问同一个域名的两个不同url最终会 ...
- ISO8583
最开始时,金融系统只有IBM这些大的公司来提供设备,象各种主机与终端等.在各个计算机设备之间,需要交换数据.我们知道数据是通过网络来传送的,而在网络上传送的数据都是基于0或1这样的二进制数据,如果没有 ...
- Python3.x:抢票
Python3.x:抢票 一个妹子叫我帮她买动车票,结果竟然没买到票:好吧,不好意思说买不到票,写个抢票程序来完成吧: 1,Chromediver安装: 因为需要chrome支持页面测试,所以需要安装 ...
- 20145216史婧瑶《Java程序设计》第5周学习总结
20145216 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生 ...