题目如下:

解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较。我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好。而后遍历nums数组的每个元素i,通过二分查找的方法在newlist中找到值比i小的元素中下标最大的那个(记为inx),那么符合条件i元素的reverse paris就是inx,累计所有的inx即可得到结果。

代码如下:

class Solution(object):
def reversePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
nl = []
for i in nums:
nl.append(2*i)
nl.sort()
res = 0
import bisect
for i in nums:
inx = bisect.bisect_left(nl,2*i)
del nl[inx]
inx = bisect.bisect_left(nl, i)
res += inx return res

【leetcode】493. Reverse Pairs的更多相关文章

  1. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  2. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  3. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  4. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  5. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  6. 【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses

    题目如下: Given a string s that consists of lower case English letters and brackets. Reverse the strings ...

  7. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  8. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  9. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

随机推荐

  1. js验证:密码只能为大写字母+小写字母+数字的8至15位字符组合

    var reg = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,15}$/; // alert(password); if(reg.test(pa ...

  2. springboot使用MockMvc测试controller

    通常,在我们平时开发项目时,如果想要输入URL对Controller进行测试,在代码编辑之后,需要重启服务器,建立http client进行测试.这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不 ...

  3. CSS3——背景 文本 字体 链接 列表样式 表格

    背景 background-color rgb(255,0,0,1)      最后一个值表示透明度,范围是 0--1 background-image 默认平铺重复显示 background-rep ...

  4. django 的多对多关系

    django里自带的多对多表创建 其实就是两个多对一关系各自关联,在第三张表上 多对多的增加 add()可以传数值 例如 add(1)或数组 add(*[2,3]) 多对多反向操作 自己创建第三张表, ...

  5. 第五周学习总结&第三次实验报告(String类的应用)

    第五周学习总结 1.学习了继承的相关知识点: (1) 继承的格式为class 子类 extends 父类{} (2) 继承实际上是通过子类去扩展父类的功能 (3) 一个子类只能继承一个父类,也就是说, ...

  6. Java第三周课程总结&实验报告一

    第三周课程总结 1.关于面向对象的一些具体内容,明白了类与对象以及Java的封装性和构造方法以及对对象匿名的相关知识. 2.this关键字,它是表示类的成员属性(变量),使用this构造方法时必须放在 ...

  7. 关于postman

    1 Get 1.1 Params 直接显示在url上,即url参数,用&分隔开. springboot中可以用@RequestParam注解获取. 1.2 Headers 1.3 Body 1 ...

  8. CentOS 7.6 RPM方式安装Oracle19c的过程

    1. 下载需要的安装包: 1.1 preinstall http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/orac ...

  9. [19/09/16-星期一] Python的运算符和条件判断语句

    一.运算符 (1)算术运算符  + 加法运算符(如果是两个字符串之间进行加法运算,则会进行拼串操作) a = 10 + 5  计算 a = 'hello' + ' ' + 'world' 拼串  - ...

  10. Linux Apache使用CGI

    CGI(Common Gateway Interface,通用网关接口)是网络服务器可以将查询传递到专门的程序中并且在网页上显示结果的标准机制.Apache等服务器默认是支持CGI的,只需要修改一下配 ...