当前位置:网站首页>Leetcode - Sword finger offer 51 Reverse pairs in an array
Leetcode - Sword finger offer 51 Reverse pairs in an array
2022-07-02 12:21:00 【Ostrich5yw】
The finger of the sword Offer 51. Reverse pairs in arrays
Title Description :[51]
Two numbers in an array , If the number in front is greater than the number in the back , Then these two numbers form a reverse order pair . Enter an array , Find the total number of reverse pairs in this array .
Key points of investigation :
The first 51 topic Merge sort implementation . During sorting , Compare the size of the left and right array elements , Calculate the number of pairs in reverse order .
The first 51 topic
class Solution {
int count = 0;
public int reversePairs(int[] nums) {
mergeSort(nums, 0, nums.length - 1);
return count;
}
public void mergeSort(int[] nums, int left, int right){
if(left >= right) return;
int mid = left + (right - left) / 2;
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
merge(nums, left, mid, right);
}
public void merge(int[] nums, int left, int mid, int right){
int[] numsTemp = new int[right - left + 1];
int pointTemp = 0, pointR = mid + 1, pointL = left;
while(pointL <= mid || pointR <= right){
if(pointR > right || pointL > mid){
while(pointL <= mid){
numsTemp[pointTemp++] = nums[pointL ++];
}
while(pointR <= right){
numsTemp[pointTemp++] = nums[pointR ++];
}
break;
}
if(nums[pointL] > nums[pointR]){
count += (mid - pointL + 1);
numsTemp[pointTemp ++] = nums[pointR++];
}else{
numsTemp[pointTemp ++] = nums[pointL++];
}
}
for(int i = left;i <= right;i ++){
nums[i] = numsTemp[i - left];
}
}
}
边栏推荐
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- drools动态增加、修改、删除规则
- Deep understanding of NN in pytorch Embedding
- Leetcode922 按奇偶排序数组 II
- Jenkins voucher management
- Performance tuning project case
- Differences between nodes and sharding in ES cluster
- 上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
- Drools executes string rules or executes a rule file
- Applet link generation
猜你喜欢
[C language] convert decimal numbers to binary numbers
Sweetheart leader: Wang Xinling
Pytorch builds LSTM to realize clothing classification (fashionmnist)
初始JDBC 编程
CDA数据分析——AARRR增长模型的介绍、使用
倍增 LCA(最近公共祖先)
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
Docker-compose配置Mysql,Redis,MongoDB
From scratch, develop a web office suite (3): mouse events
堆(優先級隊列)
随机推荐
自然语言处理系列(一)——RNN基础
Fastdateformat why thread safe
AI中台技术调研
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
Leetcode922 sort array by parity II
CDA data analysis -- Introduction and use of aarrr growth model
Codeforces 771 div2 B (no one FST, refers to himself)
还不会安装WSL 2?看这一篇文章就够了
ES集群中节点与分片的区别
[geek challenge 2019] upload
Performance tuning project case
Leetcode topic [array] -540- single element in an ordered array
Distributed machine learning framework and high-dimensional real-time recommendation system
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
mysql索引和事务
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Test shift left and right
CDA data analysis -- common knowledge points induction of Excel data processing
Writing method of then part in drools
Intel 内部指令 --- AVX和AVX2学习笔记