【leetcode】493. Reverse Pairs
题目如下:

解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较。我们可以先把数组所有元素的二倍都算出来,存入一个新的数组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的更多相关文章
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- 【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 ...
- 【LeetCode】1065. Index Pairs of a String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
随机推荐
- 全局namespace与模块内的namespace
declare global{ declare namespace xxx } 相当于 在一个js文件的顶级部分 declare namespace xxx 声明的都是全局的namespace, 如果 ...
- Delphi加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- flutter 处理dialog点击事件回调
flutter 处理dialog点击事件回调 import 'package:flutter/material.dart'; import 'package:scoped_model/scoped_m ...
- Cocos2d-X网络编程(1) 网络基本概念
网络模型 OSI层模型.TCP/IP的层模型如下所示. TCP/IP各层对应的协议如下所示. 通过初步的了解,我知道: IP协议:对应于网络层,是网络层的协议, TCP协议:对应于传输层,是传输层的协 ...
- ssh远程连接linux服务器并执行命令
详细方法: SSHClient中的方法 参数和参数说明 connect(实现ssh连接和校验) hostname:目标主机地址 port:主机端口 username:校验的用户名 password:登 ...
- powershell下载网站图片
$picurl = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10" $data = ...
- 1~n的全排列--阅文集团2018校招笔试题
题目大意:给定整数n,求出1~n的全排列 示例 输入:n=3 输出:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] import java.util.S ...
- C语言第十二周作业
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/h ...
- laravel框架源码分析(一)自动加载
一.前言 使用php已有好几年,laravel的使用也是有好长时间,但是一直对于框架源码的理解不深,原因很多,归根到底还是php基础不扎实,所以源码看起来也比较吃力.最近有时间,所以开启第5.6遍的框 ...
- Codeforces 1255E Send Boxes to Alice(前缀和+枚举+数论)
我们考虑前缀和sum[i],如果将a[i+1]中的一个塞入a[i]中,则不影响sum[i+1],但是sum[i]++,如果将a[i]中的一个塞入a[i+1],则不影响sum[i+1],但是sum[i] ...