题目如下:

解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较。我们可以先把数组所有元素的二倍都算出来,存入一个新的数组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. robotframework之使用cookies登陆

    有的系统是使用cookies保持登陆的,而RF的Request库里面没有像Selenium2Library里面一样操作cookies的关键字,若接口需要登录则只能用UI自动化先登录,然后再将cooki ...

  2. idea的掌握

    1:idea的界面了解(一般都会勾选这两项,编码比较方便) 2: 如何配置sdk(jdk,最后一个生成的是.class文件的位置) 3: 如何单个项目配置和全局配置 4:如何配置项目的jdk编译版本和 ...

  3. Spoj375 Qtree--树链剖分

    Spoj375 Qtree给一棵共有 n(n · 10000) 个结点的树, 每条边都有一个权值, 要求维护一个数据结构, 支持如下操作: 1. 修改某条边的权值; 2. 询问某两个结点之间的唯一通路 ...

  4. 学了一天的golang从入门到放弃

    Google的go就是个二货,不实用,它最多只能和c比简单.low!

  5. 【Linux开发】【Qt开发】QT 同时支持鼠标和触摸屏

    QT 同时支持鼠标和触摸屏 现在 如果我要使用鼠标 导入环境变量 export QWS_MOUSE_PROTO=MouseMan:/dev/input/mice 使用触摸屏,导入环境变量 export ...

  6. Redis进阶:Redis的主从复制机制

    Redis进阶:Redis的主从复制机制 主从复制机制介绍 单机版的Redis存在性能瓶颈,Redis通过提高主从复制实现读写分离,提高了了Redis的可用性,另一方便也能实现数据在多个Redis直接 ...

  7. go net库

    1 使用Listen函数创建一个server ln, err := net.Listen("tcp", ":8080") if err != nil { // ...

  8. Linux常用命令基础

    linux 常用指令 基础命令 宿主目录 目录结构 文件管理 目录管理 用户管理 别名管理 压缩包管理 网络设置 shell技巧 帮助方法 /表示根目录 ~表示家目录 软件的安装(光盘中的软件呢): ...

  9. spring - 第N篇 一些笔记

    1.properties文件的引入 <bean id="propertyConfigurer" class="org.springframework.beans.f ...

  10. arm初识

    一.CPU从指令集角度分类 1.1. CISC  (X86) 1.1. 1. CISC CPU 指complex instruction set computer复杂指令集CPU 1.1.2. CIS ...